这几乎是最后的手段。我一直试图让相机意图无效。我的最终目标是将文件路径传递给另一个活动。我已经尝试了几乎每个相机意图示例,似乎没有任何工作,并且当它转到startActivityForResult()时我收到一个致命的异常。它保存图像,没有任何返回null。我不知道问题是什么。代码如下
MainActivity.java
package com.example.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int SELECT_IMAGE = 1;
private static final int CAMERA_REQUEST = 1337;
private Uri imageUri;
private ImageView mImageView;
private Button mButton;
private String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.imageView1);
mButton = (Button) findViewById(R.id.button1);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// create an intent to invoke a image capture device
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File output = new File(dir,"test.jpg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(output));
path = output.getAbsolutePath();
Log.e("TEST", cameraIntent == null ? "true" : "false");
Log.e("TEST", dir == null ? "true" : "false");
Log.e("TEST", output == null ? "true" : "false");
Log.e("TEST", path == null ? "true" : "false");
System.out.println(CAMERA_REQUEST);
// start the camera activity
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
public void onSelectAnImage(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
Uri data = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath());
photoPickerIntent.setDataAndType(data, "image/*");
startActivityForResult(photoPickerIntent, SELECT_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case SELECT_IMAGE:
// get the URI of the image that the user selected.
Uri picturePath = data.getData();
System.out.println(picturePath);
Intent intent = new Intent(this, ImageInformationActivity.class);
intent.putExtra("IMAGE_FILENAME", picturePath.toString());
//intent.putExtra("uri", pass);
// start the intent.
startActivity(intent);
case CAMERA_REQUEST:
System.out.println(path);
}
}
}
}
logcat的
09-27 16:56:05.043: I/Adreno200-EGLSUB(31741): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-27 16:56:09.558: I/System.out(31741): content://media/external/images/media/1205
09-27 16:56:09.618: I/System.out(31741): /storage/sdcard0/DCIM/test.jpg
09-27 16:56:09.678: I/Adreno200-EGLSUB(31741): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-27 16:56:09.718: I/System.out(31741): android.widget.ImageView@41ef7f60
09-27 16:56:09.728: I/System.out(31741): android.os.ParcelFileDescriptor$AutoCloseInputStream@41ef9488
09-27 16:56:09.758: D/dalvikvm(31741): GC_FOR_ALLOC freed 73K, 42% free 12365K/21315K, paused 22ms, total 22ms
09-27 16:56:09.868: D/dalvikvm(31741): GC_CONCURRENT freed 3K, 19% free 40105K/49095K, paused 2ms+2ms, total 21ms
09-27 16:56:10.349: I/System.out(31741): android.graphics.Bitmap@41eed2a0
09-27 16:56:10.349: I/Choreographer(31741): Skipped 39 frames! The application may be doing too much work on its main thread.
09-27 16:56:10.379: I/Adreno200-EGLSUB(31741): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-27 16:56:12.521: I/Adreno200-EGLSUB(31741): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-27 16:56:12.571: E/SpannableStringBuilder(31741): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-27 16:56:12.571: E/SpannableStringBuilder(31741): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-27 16:56:13.522: E/TEST(31741): false
09-27 16:56:13.522: E/TEST(31741): false
09-27 16:56:13.522: E/TEST(31741): false
09-27 16:56:13.522: E/TEST(31741): false
09-27 16:56:13.522: I/System.out(31741): 1337
09-27 16:56:14.603: W/IInputConnectionWrapper(31741): showStatusIcon on inactive InputConnection
09-27 16:56:20.840: W/dalvikvm(31741): threadid=1: thread exiting with uncaught exception (group=0x411ef438)
09-27 16:56:20.850: E/AndroidRuntime(31741): FATAL EXCEPTION: main
09-27 16:56:20.850: E/AndroidRuntime(31741): java.lang.RuntimeException: Unable to resume activity {com.example.test/com.example.test.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=null} to activity {com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException
09-27 16:56:20.850: E/AndroidRuntime(31741): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2639)
09-27 16:56:20.850: E/AndroidRuntime(31741): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2667)
09-27 16:56:20.850: E/AndroidRuntime(31741): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2140)
09-27 16:56:20.850: E/AndroidRuntime(31741): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3576)
09-27 16:56:20.850: E/AndroidRuntime(31741): at android.app.ActivityThread.access$800(ActivityThread.java:143)
09-27 16:56:20.850: E/AndroidRuntime(31741): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
09-27 16:56:20.850: E/AndroidRuntime(31741): at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 16:56:20.850: E/AndroidRuntime(31741): at android.os.Looper.loop(Looper.java:137)
09-27 16:56:20.850: E/AndroidRuntime(31741): at android.app.ActivityThread.main(ActivityThread.java:4950)
09-27 16:56:20.850: E/AndroidRuntime(31741): at java.lang.reflect.Method.invokeNative(Native Method)
09-27 16:56:20.850: E/AndroidRuntime(31741): at java.lang.reflect.Method.invoke(Method.java:511)
09-27 16:56:20.850: E/AndroidRuntime(31741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
09-27 16:56:20.850: E/AndroidRuntime(31741): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
09-27 16:56:20.850: E/AndroidRuntime(31741): at dalvik.system.NativeStart.main(Native Method)
09-27 16:56:20.850: E/AndroidRuntime(31741): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=null} to activity {com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException
09-27 16:56:20.850: E/AndroidRuntime(31741): at android.app.ActivityThread.deliverResults(ActivityThread.java:3205)
09-27 16:56:20.850: E/AndroidRuntime(31741): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2626)
09-27 16:56:20.850: E/AndroidRuntime(31741): ... 13 more
09-27 16:56:20.850: E/AndroidRuntime(31741): Caused by: java.lang.NullPointerException
09-27 16:56:20.850: E/AndroidRuntime(31741): at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
09-27 16:56:20.850: E/AndroidRuntime(31741): at com.example.test.MainActivity.onActivityResult(MainActivity.java:86)
09-27 16:56:20.850: E/AndroidRuntime(31741): at android.app.Activity.dispatchActivityResult(Activity.java:5363)
09-27 16:56:20.850: E/AndroidRuntime(31741): at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
09-27 16:56:20.850: E/AndroidRuntime(31741): ... 14 more
答案 0 :(得分:3)
每次尝试记录null时,Android都会抛出NPE(这本身就是一个丑闻)。
问题是您认为结果意图包含Uri
拍摄的照片。 它没有。
相机应用无需告诉您图像的存储位置,因为您已经知道。这是你的决定,你自己选择了Uri,发送了EXTRA_OUTPUT。
更新:如果我误解了问题,请随意忽略我的回答。您可以通过提供行号来防止这种误解(至少告诉我们堆栈跟踪中提到哪些行)并且只发布实际上不起作用的代码部分。正如其他一些答案已经说明的那样,它可能也是由于path
为空。
答案 1 :(得分:1)
您的path
可以为null,因为在启动相机应用程序后可以销毁并重新创建活动。
正如dreger所说,你可以额外使用android.provider.MediaStore.html.EXTRA_OUTPUT。
这是一个片段:https://gist.github.com/koral--/6173683
答案 2 :(得分:0)
它在logcat中特别指出您返回的数据为null。在您的cameraIntent活动中,您需要专门设置要返回原始活动的结果:
Intent intent = new Intent();
intent.putExtra("result",yourresult);
setResult(RESULT_OK,intent);
finish();