2017-08-11 7 views
0

画像を選択しようとすると、「カメラ」と「ファイル」が表示されますが、選択してクリックしないとクラッシュします。 私はいくつかの答えを試したが、私はwebdevではないアンドロイドデベロッパーです。助けて!ファイルから画像を選択しないとアプリケーションがクラッシュする

これは、あなたの計算を行う前に、あなたのonActivityResult内の任意のresultCode指向のチェックを行っていないapp crashes when going back from gallery without selecting any image

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode != INPUT_FILE_REQUEST_CODE || mUploadMessage == null) { 
     super.onActivityResult(requestCode, resultCode, data); 
     return; 
    } 
    try { 
     String file_path = mCameraPhotoPath.replace("file:",""); 
     File file = new File(file_path); 
     size = file.length(); 

    }catch (Exception e){ 
     Log.e("Error!", "Error while opening image file" + e.getLocalizedMessage()); 
    } 

    if (data != null || mCameraPhotoPath != null) { 
     Integer count = 1; 
     ClipData images = null; 
     try { 
      images = data.getClipData(); 
     }catch (Exception e) { 
      Log.e("Error!", e.getLocalizedMessage()); 
     } 

     if (images == null && data != null && data.getDataString() != null) { 
       count = data.getDataString().length(); 
     } else if (images != null) { 
       count = images.getItemCount(); 
     } 
     Uri[] results = new Uri[count]; 
     // Check that the response is a good one 
     if (resultCode == Activity.RESULT_OK) { 
      if (size != 0) { 
       // If there is not data, then we may have taken a photo 
       if (mCameraPhotoPath != null) { 
        results = new Uri[]{Uri.parse(mCameraPhotoPath)}; 
       } 
      } else if (data.getClipData() == null) { 
       results = new Uri[]{Uri.parse(data.getDataString())}; 
      } else { 

       for (int i = 0; i < images.getItemCount(); i++) { 
        results[i] = images.getItemAt(i).getUri(); 
       } 
      } 
     } 

     mUploadMessage.onReceiveValue(results); 
     mUploadMessage = null; 
    } 
} 
+2

あなたは – GuilhermeFGL

答えて

5

に似ています。イメージを選択せず​​に戻ると、resultCodeはRESULT_CANCELLEDdataはnullです。次のようにdata上の任意の操作が常に

が行う、nullポインタ例外が発生しますことを投稿 -

ほとんど
if (resultCode==RESULT_OK){ 
    // do stuff here that is the part of your try catch block 
} 

あなたがあるデータにdata.getDataString()を実行しようとするとき、あなたのコードがクラッシュしますいない。また、これがあなたのtryブロックの外側にあることを考慮すると、決してキャッチされず、アプリケーションがクラッシュすることもありません。

+0

'mCameraPhotoPath.replace(「ファイル:」、「」)されるエラーとあなたの質問を更新活動は –

+2

1があるが死亡したときBundle''でstorredない場合は 'も危険です。それは終わり近くにあり、おそらく遅くまでそれをするが、1つがある – litelite

+0

ええ、私はそれを今見た。遅すぎる。ありがとう@litelite –

関連する問題