私は2つの画像ボタンを持っています.1つはギャラリーから写真を撮って別のアクティビティに表示するためのボタンです。これは正常に動作します。それは、高品質の写真を表示します。しかし、カメラから写真を撮ろうとすると、その写真が別のアクティビティに表示されると、エラーが表示されます。ここに私のコード MainActivity.javaは次のとおりです。画像ビューで別のアクティビティでカメラから取り込んだ画像を読み込む方法は?
private static final int REQUEST_CAMERA = 1;
private static int SELECT_FILE = 1;
public static Uri selectedImageURI;
if (take_photo != null) {
take_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,REQUEST_CAMERA);
}
});
}
//to get the photo from gallery
if (get_photo != null) {
get_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i,SELECT_FILE);
}
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_FILE) {
selectedImageURI = data.getData();
File imageFile = new File(getRealPathFromURI(selectedImageURI));
Intent intent = new Intent(this, ShowPhotoActivity.class);
intent.putExtra("imagePath", imageFile.toString());
startActivity(intent);}
else if (requestCode != RESULT_CANCELED)
{
if(requestCode == REQUEST_CAMERA){
Uri selectImageURI = data.getData();
File imgFile = new File(getRealPathFromURI(selectImageURI));
Intent i = new Intent(this,ShowPhotoActivity.class);
i.putExtra("imagePath",imgFile.toString());
startActivity(i);
}
} }}
private String getRealPathFromURI(Uri contentURI){
String result;
Cursor cursor = getContentResolver().query(contentURI,null,null,null,null);
if(cursor == null){
result = contentURI.getPath();
}else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}
showPhotoActivity.java:
String image_path = getIntent().getStringExtra("imagePath");
Bundle extras = getIntent().getExtras();
Uri uri = Uri.parse(extras.getString("imagePath"));
if (showPhoto != null) {
Bitmap bm = BitmapFactory.decodeFile(uri.getPath());
showPhoto.setImageBitmap(bm);
}
のmanifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
logcat:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.gentaliu.photoeditor/com.example.gentaliu.photoeditor.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3840)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3883)
at android.app.ActivityThread.access$1700(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1426)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1477)
at android.content.ContentResolver.query(ContentResolver.java:473)
at android.content.ContentResolver.query(ContentResolver.java:426)
at com.example.ga.photoeditor.MainActivity.getRealPathFromURI(MainActivity.java:96)
at com.example.ga.photoeditor.MainActivity.onActivityResult(MainActivity.java:69)
at android.app.Activity.dispatchActivityResult(Activity.java:6320)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3836)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3883)
at android.app.ActivityThread.access$1700(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1426)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
9月16日00 :46:51.149 27066-27066/c om.example.ga.photoeditor I/Process:送信信号。 PID:27066 SIG:9
単にあなたの第二の活動であなたのパスをデコードします。 – Piyush
コードが見えましたか?私はそれをしたが、それは働かないから。 –