1

ギャラリーから画像を取り込むか、カメラから画像を取り込むためのボタンがあるアプリを開発中です。ギャラリーから画像を選択すると、最後にエラー画像が表示されなくなりますが、カメラアプリから画像をキャプチャするとクラッシュします。続きカメラから画像を取り込んでいてもギャラリーから画像を取り出すときに問題は発生しません

は誤りです:ギャラリー/カメラからの画像を撮像する

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=66, result=-1, data=null} to activity 

{com.byteshaft.prospectform/com.byteshaft.prospectform.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference 
                       at android.app.ActivityThread.deliverResults(ActivityThread.java:4297) 
                       at android.app.ActivityThread.handleSendResult(ActivityThread.java:4347) 
                       at android.app.ActivityThread.-wrap20(ActivityThread.java) 
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557) 
                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                       at android.os.Looper.loop(Looper.java:173) 
                       at android.app.ActivityThread.main(ActivityThread.java:6459) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:938) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828) 
                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference 
                       at com.byteshaft.prospectform.MainActivity.onActivityResult(MainActivity.java:400) 
                       at android.app.Activity.dispatchActivityResult(Activity.java:6926) 
                       at android.app.ActivityThread.deliverResults(ActivityThread.java:4293) 
                       at android.app.ActivityThread.handleSendResult(ActivityThread.java:4347)  
                       at android.app.ActivityThread.-wrap20(ActivityThread.java)  
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)  
                       at android.os.Handler.dispatchMessage(Handler.java:102)  
                       at android.os.Looper.loop(Looper.java:173)  
                       at android.app.ActivityThread.main(ActivityThread.java:6459)  
                       at java.lang.reflect.Method.invoke(Native Method)  
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:938)  
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)  

マイコード:

public void openImageIntent() { 

     File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 

     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     String fname = "Prospect-form" + timeStamp; 

     final File sdImageMainDirectory = new File(storageDir, fname); 
     outputFileUri = Uri.fromFile(sdImageMainDirectory); 

     // Camera. 
     final List<Intent> cameraIntents = new ArrayList<>(); 
     final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     final PackageManager packageManager = getPackageManager(); 
     final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0); 
     for(ResolveInfo res : listCam) { 
      final String packageName = res.activityInfo.packageName; 
      final Intent intent = new Intent(captureIntent); 
      intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); 
      intent.setPackage(packageName); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
      cameraIntents.add(intent); 
     } 


     //Gallery. 
     Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

     // Filesystem. 
     final Intent fsIntent = new Intent(); 
     fsIntent.setType("*/*"); 
     fsIntent.setAction(Intent.ACTION_GET_CONTENT); 
     cameraIntents.add(fsIntent); 



     //Create the Chooser 
     final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source"); 
     chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()])); 

     startActivityForResult(chooserIntent, 66); 
    } 

onActivityResultコード: `

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(resultCode == Activity.RESULT_OK) 
      switch (requestCode){ 
       case 5: 
        Uri selectedImage = data.getData(); 
        try { 
         Bitmap bitmap = MediaStore.Images.Media.getBitmap(MainActivity.this.getContentResolver(), selectedImage); 
         Bitmap resizedBitMap; 
         resizedBitMap = Bitmap.createScaledBitmap(bitmap, 100, 100, true); 
         imageView.setImageBitmap(resizedBitMap); 
         System.out.println(Helpers.getPath(MainActivity.this, selectedImage)); 
         imagePath = Helpers.getPath(MainActivity.this, selectedImage); 

        } catch (IOException e) { 
         Log.i("TAG", "Some exception " + e); 
        } 
        break; 
      } 
    }` 

いくつかは非常にあります。便利な答えはstackoverflowで私は私のprobleを解決することができませんm。私がギャラリーから画像を選択すると、カメラから画像をキャプチャするときにエラーが発生します。

提案がありますか?

答えて

1

カメラからの画像とギャラリーからの画像を選択しても同じ結果が返されない場合は、ユーザーが意図を見て選択した画像を確認する必要があります。あなたはこのようにすることができます

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == mRequestCode) { 
      if (resultCode == Activity.RESULT_OK) { 
       boolean isCamera = true; 
       if (data != null && data.getData() != null) { 
        String action = data.getAction(); 
        isCamera = MediaStore.ACTION_IMAGE_CAPTURE.equals(action); 
       } 

       try { 
        Uri uriFileSrc = isCamera ? mOutputFileUri : data.getData(); 

        //Do what do you need with the Uri 

       } catch (Exception ex) { 
        Toast.makeText(mActivity, R.string.error_creating_file, Toast.LENGTH_SHORT).show(); 
       } 
      } 
     } 
} 
+0

パーフェクト! それは働いて、ありがとう! –

関連する問題