2
 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().this line 中显示错误

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
4

2 回答 2

0

The way you set the output file is fishy.

Try like this:

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);
}

Also make sure that using external storage is declared in your Android manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

For more info see this page in the docs: http://developer.android.com/training/camera/photobasics.html

于 2013-02-07T13:30:10.473 回答
0

我打赌你忘了插入 SD 卡

编辑: 您可以在活动中使用此代码访问它:

@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;
}
于 2013-02-11T07:37:41.787 回答