该项目功能就是从本地选择一个一个图片,然后shuffle 图片下面贴所有的代码package com.example.gsbtest;import java.io.FileNotFoundException;
import java.io.InputStream;import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;public class MainActivity extends Activity { Button btn0,btn1;
TextView Text1;
Bitmap bmp, bitmap ;//选择了图片后调用
ImageView imageView;    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btn0 = (Button)findViewById(R.id.b01);
        btn0.setText("select image");
        btn1 = (Button)findViewById(R.id.sufflebtn);
        
        btn0.setOnClickListener(new OnClickListener(){
        
         public void onClick(View v){
         Intent intent = new Intent();
         intent.setType("image/*");
         intent.setAction(Intent.ACTION_GET_CONTENT);
         startActivityForResult(intent,1);
         }
        });
        
        btn1.setOnClickListener(new OnClickListener(){
       
         @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
         Text1 = (TextView)findViewById(R.id.Txt1);
         if(bitmap==null || bitmap.isRecycled() ) 
         return;
//         try{
         suffleImageRGB(bitmap);
//         }catch(Exception ex){
//         Log.e("Exception", ex.getMessage(),ex);  
//         }
}
        });
        
    }    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {  
        if (resultCode == RESULT_OK) {  
            Uri uri = data.getData();  
            Log.e("uri", uri.toString());  
            android.content.ContentResolver cr = this.getContentResolver();  
            try {  
                bitmap = BitmapFactory.decodeStream((InputStream) cr.openInputStream(uri));  
                imageView = (ImageView) findViewById(R.id.iv01);  
                /* 将Bitmap设定到ImageView */  
                imageView.setImageBitmap(bitmap);  
            } catch (FileNotFoundException e) {  
                Log.e("Exception", e.getMessage(),e);  
            }  
        }  
        super.onActivityResult(requestCode, resultCode, data);  
    }  
    
    void suffleImageRGB(Bitmap bmp) 
    {    
    
    
     int w = bmp.getWidth();     
     int h = bmp.getHeight();     
     int color=0,r=0,g=0,b=0;     
     for(int x=0; x<w; x++)
     {          
     for(int y=0 ;y<h; y++)
     {             
     color = bmp.getPixel(x, y);       
     r = Color.red(color);             
     g = Color.green(color);             
     b = Color.blue(color);  
     bmp.setPixel(x,y,Color.argb(Color.alpha(color), b,g ,r ) );//R->G->B->R        }    }   } 
     imageView.setImageBitmap(bmp);
     }
     }
    }
        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

解决方案 »

  1.   

    从下面代码解码出来的Bitmap不一定是Mutable的bitmap = BitmapFactory.decodeStream((InputStream) cr.openInputStream(uri));只有Mutable的Bitmap才能使用setPixel方法bmp.setPixel(x,y,Color.argb(Color.alpha(color), b,g ,r ) );//R->G->B->R可以使用Bitmap.copy方法复制一个mutable的bitmap出来
      

  2.   

    bitmap = BitmapFactory.decodeStream((InputStream) cr.openInputStream(uri)); 
    这段是从本地获取图片的相关代码片段那既然是这样,那怎么修改我上面的代码呢? 我的意思是从本地手机获取图片之后shuffle这个获取到的图片
      

  3.   

    我用try catch 套住 suffleImageRGB(bitmap);,然后运行的时候日志文件里提示如下04-10 02:54:40.629: D/gralloc_goldfish(792): Emulator without GPU emulation detected.
    04-10 02:56:26.300: E/uri(792): content://media/external/images/media/18
    04-10 02:56:26.660: D/dalvikvm(792): GC_FOR_ALLOC freed 80K, 8% free 2741K/2948K, paused 204ms, total 234ms
    04-10 02:56:26.709: I/dalvikvm-heap(792): Grow heap (frag case) to 3.961MB for 1228816-byte allocation
    04-10 02:56:26.890: D/dalvikvm(792): GC_FOR_ALLOC freed 4K, 6% free 3937K/4152K, paused 136ms, total 136ms
    04-10 02:56:32.230: E/Exception(792): null
    04-10 02:56:32.230: E/Exception(792): java.lang.IllegalStateException各位请帮忙
      

  4.   

    就像2楼说的,先复制一个出来,然后把复制出来的这个对象传出来做你的处理如果还是不行的话,你把工程传过来我帮你改改试试吧[email protected]