public void startCamera() {
Log.d("TDM_CAMERA", "Starting camera on the phone...");
String fileName = "testphoto.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image capture by camera");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, 1337);
}
log cat在imageUri = getContentResolver()。此行
中显示错误02-08 07:32:11.505: E/AndroidRuntime(2136): FATAL EXCEPTION: main
02-08 07:32:11.505: E/AndroidRuntime(2136): java.lang.UnsupportedOperationException: Unknown URI: content://media/external/images/media
02-08 07:32:11.505: E/AndroidRuntime(2136): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:169)
02-08 07:32:11.505: E/AndroidRuntime(2136): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
02-08 07:32:11.505: E/AndroidRuntime(2136): at android.content.ContentProviderProxy.insert(ContentProviderNative.java:420)
02-08 07:32:11.505: E/AndroidRuntime(2136): at android.content.ContentResolver.insert(ContentResolver.java:866)
02-08 07:32:11.505: E/AndroidRuntime(2136): at com.Ibetter.tdm.ReqEditActivity.startCamera(ReqEditActivity.java:597)
02-08 07:32:11.505: E/AndroidRuntime(2136): at com.Ibetter.tdm.ReqEditActivity$4.onClick(ReqEditActivity.java:107)
02-08 07:32:11.505: E/AndroidRuntime(2136): at android.view.View.performClick(View.java:4202)
02-08 07:32:11.505: E/AndroidRuntime(2136): at android.view.View$PerformClick.run(View.java:17340)
02-08 07:32:11.505: E/AndroidRuntime(2136): at android.os.Handler.handleCallback(Handler.java:725)
02-08 07:32:11.505: E/AndroidRuntime(2136): at android.os.Handler.dispatchMessage(Handler.java:92)
02-08 07:32:11.505: E/AndroidRuntime(2136): at android.os.Looper.loop(Looper.java:137)
02-08 07:32:11.505: E/AndroidRuntime(2136): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-08 07:32:11.505: E/AndroidRuntime(2136): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 07:32:11.505: E/AndroidRuntime(2136): at java.lang.reflect.Method.invoke(Method.java:511)
02-08 07:32:11.505: E/AndroidRuntime(2136): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-08 07:32:11.505: E/AndroidRuntime(2136): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-08 07:32:11.505: E/AndroidRuntime(2136): at dalvik.system.NativeStart.main(Native Method)
02-08 07:32:13.935: I/Process(2136): Sending signal. PID: 2136 SIG: 9
02-08 07:32:14.835: E/Trace(2175): error opening trace file: No such file or directory (2)
02-08 07:32:14.835: W/Trace(2175): Unexpected value from nativeGetEnabledTags: 0
答案 0 :(得分:0)
您设置输出文件的方式很可疑。
试试这样:
public void startCamera() throws IOException {
Log.d("TDM_CAMERA", "Starting camera on the phone...");
File photosDir = new File(Environment.getExternalStorageDirectory(), "photos");
if (!photosDir.isDirectory()) {
photosDir.mkdirs();
}
File imageFile = File.createTempFile("testphoto", ".jpg", photosDir);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, 1337);
}
还要确保在Android清单中声明使用外部存储:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
有关详细信息,请参阅文档中的此页面: http://developer.android.com/training/camera/photobasics.html
答案 1 :(得分:0)
我打赌你forgot to insert the SD card
修改强> 您可以在活动中使用此代码访问它:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.capture);
Button capture = (Button) findViewById(R.id.capture_button);
capture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// We use the stock camera app to take a photo
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
startActivityForResult(intent, TAKE_PHOTO_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Uri imagePath = getImageUri();
doSomething();
}
}
/**
* Get the uri of the captured file
* @return A Uri which path is the path of an image file, stored on the dcim folder
*/
private Uri getImageUri() {
// Store image in dcim
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM", CAPTURE_TITLE);
Uri imgUri = Uri.fromFile(file);
return imgUri;
}