我正在尝试通过Android模拟器捕获并保存图像,正在捕获图像,但正在保存的文件已损坏。
可能导致这种情况的原因是什么?任何人都可以帮我识别可能的错误点吗?
以下是我的代码:
**public void onCreate**(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
**Intent** intent = new **Intent**("android.media.action.IMAGE_CAPTURE");
try {
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
intent.putExtra(**MediaStore**.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
@Override
**protected void onActivityResult**(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
StoreImage(this, data.getData(),
mediaFile);
finish();
}
else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
}
else {
finish();
try {
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
堆栈跟踪低于::
04-05 23:55:40.369: E/AndroidRuntime(534): FATAL EXCEPTION: main
04-05 23:55:40.369: E/AndroidRuntime(534): java.lang.RuntimeException: Unable to start activity ComponentInfo{camera.android/camera.android.CameraActivity}: java.lang.NullPointerException: file
04-05 23:55:40.369: E/AndroidRuntime(534): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
04-05 23:55:40.369: E/AndroidRuntime(534): Caused by: java.lang.NullPointerException: file
04-05 23:55:40.369: E/AndroidRuntime(534): at android.net.Uri.fromFile(Uri.java:441)
04-05 23:55:40.369: E/AndroidRuntime(534): at camera.android.CameraActivity.getOutputMediaFileUri(CameraActivity.java:72)
04-05 23:55:40.369: E/AndroidRuntime(534): at camera.android.CameraActivity.onCreate(CameraActivity.java:34)
04-05 23:55:40.369: E/AndroidRuntime(534): at android.app.Activity.performCreate(Activity.java:4465)
04-05 23:55:40.369: E/AndroidRuntime(534): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-05 23:55:40.369: E/AndroidRuntime(534): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
答案 0 :(得分:4)
交换这一行,
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
到
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
首先检查子活动是否成功完成其工作。
答案 1 :(得分:4)
检查您是否在清单文件::
中添加了以下权限<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
也在
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
我认为你必须附上你想要保存的文件名,只给出路径是不够的....
答案 2 :(得分:1)
“@ Agarwal:关于调用onActivityResult”
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
StoreImage(this, data.getData(), mediaFile);
finish();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
finish();
try {
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
“如果结果成功,则来自实习生的数据存储在n文件中,该文件被创建并保存为jpeg格式”
public static void StoreImage(Context mContext, Uri imageLoc, File imageDir) {
Bitmap bm = null;
try {
bm = Media.getBitmap(mContext.getContentResolver(), imageLoc);
FileOutputStream out = new FileOutputStream(imageDir);
bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
bm.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 3 :(得分:0)
尝试以下代码,
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File file=getOutputMediaFile(1);
picUri = Uri.fromFile(file); // create
i.putExtra(MediaStore.EXTRA_OUTPUT,picUri); // set the image file
startActivityForResult(i, CAPTURE_IMAGE);
getOutputMediaFile(int)将在哪里,
/** Create a File for saving an image */
private File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyApplication");
/**Create the storage directory if it does not exist*/
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}
/**Create a media file name*/
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == 1){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".png");
} else {
return null;
}
return mediaFile;
}
最后,
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
Intent i;
switch (requestCode) {
case CAPTURE_IMAGE:
//THIS IS YOUR Uri
Uri uri=picUri;
break;
}
}
}
欢呼....:)