这是我目前的代码:
public void onClick(View v) {
final Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
final Intent gallIntent=new Intent(Intent.ACTION_GET_CONTENT);
gallIntent.setType("image/*");
final Intent camIntent = new Intent("android.media.action.IMAGE_CAPTURE");
pickIntent.putExtra(Intent.EXTRA_INTENT, camIntent);
pickIntent.putExtra(Intent.EXTRA_INTENT, gallIntent);
pickIntent.putExtra(Intent.EXTRA_TITLE, "Select Source");
startActivityForResult(pickIntent, 0);
if (bitmap == null) {
Toast.makeText(getApplicationContext(),
"Please select image", Toast.LENGTH_SHORT).show();
} else {
dialog = ProgressDialog.show(CreatePod.this, "Uploading",
"Please wait...", true);
//new ImageUploadTask().execute();
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
Toast toast = Toast.makeText(this,"camera cancelled", 10000);
toast.show();
return;
}
// lets check if we are really dealing with a picture
if (requestCode == 0 && resultCode == RESULT_OK)
{
Bundle extras = data.getExtras();
Bitmap b = (Bitmap) extras.get("data");
//setContentView(R.layout.main);
imgView.setImageBitmap(b);
// save image to gallery
String timestamp = Long.toString(System.currentTimeMillis());
MediaStore.Images.Media.insertImage(getContentResolver(), b, timestamp, timestamp);
}
}
这给出了Gallery和Camera选项(实际上,它显示了一个不支持的设备代替相机。如果我点击它,它会给出NullPointerException并崩溃。这是正确的方法吗?或者我应该使用PackageManger ?如果是,那怎么做?
答案 0 :(得分:2)
查看您的代码,我认为您缺乏基本的Android知识,
错过使用onActivityResult()
,它是Activity类的一种方法,从onClick()中取出它
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
}
只需检查基本的Android活动方法以及如何使用它们。
更新