我尝试使用此代码从Android相机获取图片,但我会得到错误,请您帮助我实现此功能,提前致谢
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//try{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_IMAGE)
if (resultCode == Activity.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]);
Log.d("filecolumn : ",filePathColumn.toString());
String picturePath = cursor.getString(columnIndex);
cursor.close();
Intent i = new Intent(getApplicationContext(), CustomizeActivity.class);
i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
mCurrentPhotoPath);
i.putExtra("type", "album");
i.putExtra("photo", picturePath);
Log.d("getPath : ",picturePath);
startActivity(i);
overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
Log.d("photo : ",selectedImage.getPath());
// TODO Do something with the select image URI
}
if(requestCode == CAMERA_PIC_REQUEST)
{
Log.v("camera", "cam");
if (resultCode == Activity.RESULT_OK) {
Log.v("camera", "result ok");
Intent i = new Intent(getApplicationContext(), CustomizeActivity.class);
i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
mCurrentPhotoPath);
i.putExtra("type", "photo");
i.putExtra("photo", mCurrentPhotoPath);
startActivity(i);
overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
}
}
}
错误日志:
01-22 15:53:55.375: D/szipinf(810): Initializing inflate state
01-22 15:53:55.386: V/photo path(810): photoPath : null
01-22 15:53:55.386: W/System.err(810): java.lang.NullPointerException: file == null
01-22 15:53:55.386: W/System.err(810): at java.io.FileInputStream.<init>(FileInputStream.java:76)
01-22 15:53:55.386: W/System.err(810): at java.io.FileInputStream.<init>(FileInputStream.java:132)
01-22 15:53:55.386: W/System.err(810): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:362)
01-22 15:53:55.386: W/System.err(810): at com.odacia.appcarnaval.CustomizeActivity.onCreate(CustomizeActivity.java:181)
01-22 15:53:55.386: W/System.err(810): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-22 15:53:55.386: W/System.err(810): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
android.intent.action.MEDIA_UNMOUNTED
答案 0 :(得分:0)
尝试使用此代码用于相机,它适用于我,2.2至4.0.1。
private static final int PICK_Camera_IMAGE = 2;
camera.setOnClickListener(new OnClickListener() {
//
public void onClick(View v) {
String name = dateToString(new Date(), "yyyy-MM-dd-hh-mm-ss");
destination = new File(Environment
.getExternalStorageDirectory(), name + ".jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(destination));
startActivityForResult(intent, PICK_Camera_IMAGE);
}
});
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
Uri selectedImageUri = null;
String filePath = null;
switch (requestCode) {
case PICK_Camera_IMAGE:
if (resultCode == RESULT_OK) {
Toast.makeText(Setting.this, "staRtttt", Toast.LENGTH_LONG)
.show();
try {
in = new FileInputStream(destination);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10;
imagePath = destination.getAbsolutePath();
mimagepath.setText(imagePath);
// Toast.makeText(Setting.this, "" + imagePath + "",Toast.LENGTH_LONG).show();
Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// CompressFormat set up to JPG, you can change to PNG or
// whatever you
// want;
bmp.compress(CompressFormat.JPEG, 100, bos);
bmp.compress(CompressFormat.JPEG, 100, bos);
// **********************
// globaly..................................
data = bos.toByteArray();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken",
Toast.LENGTH_SHORT).show();
}
break;
}
}