2016-08-18 74 views
-1

我正在開發一個android應用程序,當我按下一個按鈕拍攝照片時,它會一直崩潰,然後應該將照片保存到文件夾並在應用程序中顯示縮略圖,但是一旦拍攝照片,應用程序崩潰,然而將照片保存在所需的文件夾中。在開始實施應用程序的照片保存部分之前顯示縮略圖。爲什麼當我嘗試訪問它的意圖數據時,我的應用程序崩潰?

應用崩潰,一旦它得到的代碼塊(onActivityResult重寫功能):

 if(data.getData() == null) { 
      thumbnail.add((Bitmap)data.getExtras().get("data")); 
     } 
     if(thumbnail.get(0) != null && thumbnail.size() > 0) 
     { 
      for(int i=0; i < thumbnail.size();i++){ 
       //Toast.makeText(getApplicationContext(), Integer.toString(thumbnail.size()), Toast.LENGTH_SHORT).show(); 
       createPhotoThumbnail(thumbnail); 
      } 
     } 

這是相機調度意圖功能:

private void dispatchCameraIntent(){ 
    Intent takePicIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    if (takePicIntent.resolveActivity(getPackageManager()) != null) { 
     File photoFile = null; 
     try { 
      photoFile = createImageFile(); 
     } catch (IOException ex) { 
      // Error occurred while creating the Files 
      Toast.makeText(this, "Could not create file", Toast.LENGTH_SHORT).show(); 
     } 
     // Continue only if the File was successfully created 
     if (photoFile != null) { 
      Uri photoURI = Uri.fromFile(photoFile); 

      takePicIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
      takePicIntent.putExtra(android.provider.MediaStore.EXTRA_SIZE_LIMIT, "720000"); 
      startActivityForResult(takePicIntent, REQUEST_IMAGE_CAPTURE); 
      setResult(RESULT_OK, takePicIntent); 
     } 
    } else 
    { 
     //Make a toast if there is no camera app installed. 
     Toast toast = Toast.makeText(this,"No program to take pictures",Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
} 

我已添加logcat:

FATAL EXCEPTION: main 
Process: lt.vilniausbaldai.vilniausbaldaiofflinedefektai, PID: 14330 
Theme: themes:{} 
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {lt.vilniausbaldai.vilniausbaldaiofflinedefektai/lt.vilniausbaldai.vilniausbaldaiofflinedefektai.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3733) 
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776) 
    at android.app.ActivityThread.-wrap16(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5461) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102) 
    Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 
    at java.util.ArrayList.get(ArrayList.java:308) 
    at lt.vilniausbaldai.vilniausbaldaiofflinedefektai.MainActivity.onActivityResult(MainActivity.java:135) 
    at android.app.Activity.dispatchActivityResult(Activity.java:6456) 
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3729) 
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)  
    at android.app.ActivityThread.-wrap16(ActivityThread.java)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:148)  
    at android.app.ActivityThread.main(ActivityThread.java:5461)  
    at java.lang.reflect.Method.invoke(Native Method) 

createPhotoThumbnail方法:

private void createPhotoThumbnail(ArrayList<Bitmap> thumbnail){ 
    this.imageGrid = (GridView) findViewById(R.id.gridView); 
    this.bitmapList = new ArrayList<>(); 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 

    File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg"); 

    try { 
     file.createNewFile(); 
     FileOutputStream fo = new FileOutputStream(file); 
     fo.write(bytes.toByteArray()); 
     fo.close(); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     for(int i = 0; i < thumbnail.size(); i++) { 
      thumbnail.get(i).compress(Bitmap.CompressFormat.JPEG, 100, bytes); 

      this.bitmapList.add(thumbnail.get(i)); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    this.imageGrid.setAdapter(new ImageAdapter(this, this.bitmapList)); 
    imageGrid.invalidate(); 
} 

createImageFile方法:

private File createImageFile() throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date()); 
    //String timeStamp = "picTesting"; 
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 

    File image = File.createTempFile(
      timeStamp, /* prefix */ 
      ".png",   /* suffix */ 
      storageDir  /* directory */ 
    ); 

    // Save a file: path for use with ACTION_VIEW intents 
    mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 
    //Toast.makeText(this, mCurrentPhotoPath, Toast.LENGTH_LONG).show(); 
    return image; 
} 
+0

請出示您的堆棧跟蹤 –

+0

已添加的權限? – Alikbar

+0

是的,我已經添加了android.permission.CAMERA和WRITE_EXTERNAL_STORAGE,還包括

回答

0

錯誤在該行occures很可能是:

if(thumbnail.get(0) != null && thumbnail.size() > 0) 

這是因爲首先你嘗試 獲取索引爲0的元素之後,您會檢查是否存在某些元素。

如果更改這些條件的順序,應該很好地工作:

if(thumbnail.size() > 0 && thumbnail.get(0) != null) 

有了這個,你先檢查是否有甚至一些元素,且僅當有,你嘗試獲得元素在索引0並檢查它是否爲null

+0

不幸的是,這並沒有解決問題,保存照片後,應用程序仍然崩潰。它也崩潰,如果它通過這個如果:「if(data.getData()== null){」 –

+0

你有沒有像以前一樣的logcat? – Bobby

+0

logcat是相同的,我甚至再次嘗試兩次以確保。 –

0

試試這個:

if(data != null){  
     Bundle extras = data.getExtras(); 
if (requestCode == REQUEST_IMAGE_CAPTURE && resultcode == RESULT_OK){ 

      if (extras.containsKey("data")) { 
        bitmap = (Bitmap) extras.get("data"); 
        thumbnail.add(bitmap); 
       } 
       if(thumbnail.get(0) != null && thumbnail.size() > 0) 
       { 
        for(int i=0; i < thumbnail.size();i++){ 
         //Toast.makeText(getApplicationContext(), Integer.toString(thumbnail.size()), Toast.LENGTH_SHORT).show(); 
         createPhotoThumbnail(thumbnail); 
        } 
       } 
    } 
} 
+0

可悲的是沒有工作,這裏是我的logcat:http://pastebin.com/wrdGm0nZ –

+0

嘗試更新的答案 – Jas

+0

這似乎阻止應用程序崩潰,照片仍然保存,但是添加縮略圖的整個代碼塊永遠不會運行,應用程序永遠不會過去「if(data!= null){」 –

相關問題