在Android Eclipse中旋转图像时出错
这是我的主要代码在Android Eclipse中旋转图像时出错,android,eclipse,Android,Eclipse,这是我的主要代码 public class MainActivity extends Activity { private static int RESULT_LOAD_IMAGE = 1; private Spinner spinner1; Bitmap b; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setConten
public class MainActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
private Spinner spinner1;
Bitmap b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnSpinnerItemSelection();
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ImageView imageView = (ImageView) findViewById(R.id.imgView);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
b = BitmapFactory.decodeFile(picturePath);
imageView.setImageBitmap(b);
}
}
public void addListenerOnSpinnerItemSelection() {
ImageView imageView = (ImageView) findViewById(R.id.imgView);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelected(imageView, this , b));
}
}
这是另一节课
public class CustomOnItemSelected implements OnItemSelectedListener {
ImageView i;
Context c;
private Bitmap myBitmap;
CustomOnItemSelected(ImageView i,Context c, Bitmap b)
{
this.i = i;
this.c = c;
myBitmap = b;
}
//private static final float ROTATE_FROM = 0.0f;
//专用静态最终浮动旋转至=-10.0f*360.0f
已选择公共位置(AdapterView父项、视图、整数位置、长id){
Toast.makeText(parent.getContext(),
OnItemSelectedListener:+parent.getItemAtPosition(pos.toString(),
吐司。长度(短)。show()
它给出了空指针异常错误..请帮助我,出了什么问题?在调用b(位图)之前,在addListenerOnSpinnerItemSelection()中创建了CustomOnItemSelected对象,该对象从onCreate调用。因此,将b分配给myBitmap时为空
将addListenerOnSpinnerItemSelection()移动到onActivityResult()调用要确保在调用CustomOnItemSelected构造函数之前创建位图。否,但我要做的是,首先加载图像。然后,如果要执行任何活动,则只需单击任何操作,如“旋转”或“放大”。最初,我使用了不同的xml文件进行“旋转”,然后工作正常。。。
if(parent.getItemAtPosition(pos).toString().equals("Rotate"))
{
Matrix mat = new Matrix();
mat.postRotate(90);
Bitmap bMapRotate = Bitmap.createBitmap(myBitmap, 0, 0, myBitmap.getWidth(), myBitmap.getHeight(), mat, true); //GIVING ERROR ON THIS LINE...
i.setImageBitmap(bMapRotate);
}