2012-12-26 44 views
2

我正在使用相机或图库为我的应用拍照。但有时在点击相机图像然后切换到图库后,图库崩溃。下面图库(process com.cooliris.media)已经意外停止

代码给出:

case R.id.etUploadImage: 
     Log.d(TAG, " add photo"); 
     if (!Environment.getExternalStorageState().equals(
       Environment.MEDIA_MOUNTED)) { 
      Toast.makeText(getApplicationContext(), "sdcard not mounted", 
        Toast.LENGTH_SHORT).show(); 
      break; 
     } 
     AlertDialog.Builder photoDialog = new AlertDialog.Builder(this); 
     photoDialog 
       .setTitle("Photo source") 
       .setCancelable(true) 

       .setPositiveButton("Open Gallery", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, 
            int id) { 
           startActivityForResult(
             new Intent(
               Intent.ACTION_PICK, 
               android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI), 
             54); 
          } 
         }) 

       .setNegativeButton("Open Camera", 
         new DialogInterface.OnClickListener() { 

          public void onClick(DialogInterface dialog, 
            int id) { 
           //String fileName = "temp.jpg"; 
           Date date = new Date(); 
           DateFormat df = new SimpleDateFormat("-mm-ss"); 

           String newPicFile = "Bild"+ df.format(date) + ".jpg"; 
           String outPath = "/sdcard/" + newPicFile; 
           File outFile = new File(outPath); 

           ContentValues values = new ContentValues(); 
           values.put(MediaStore.Images.Media.TITLE, 
             outFile.getAbsolutePath()); 
           mCapturedImageURI = getContentResolver() 
             .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
               values); 

           Intent intent = new Intent(
             MediaStore.ACTION_IMAGE_CAPTURE); 
           intent.putExtra(MediaStore.EXTRA_OUTPUT, 
             mCapturedImageURI); 

           startActivityForResult(intent, 96); 

          } 
         }); 
     photoDialog.show(); 
     break; 
    } 

和onActivityResult()是:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 54 && resultCode == RESULT_OK) { 
     if(data !=null){ 
     fileName = getRealPathFromURI(data.getData()); 
     mUploadImage.setText(getStringNameFromRealPath((String) fileName)); 
     } else { 
      Toast.makeText(getApplicationContext(),"Please select another image.", Toast.LENGTH_SHORT).show(); 
     } 
    } else if (requestCode == 96 && resultCode != 0) { 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(mCapturedImageURI, projection, null, 
       null, null); 
     int column_index_data = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     fileName = cursor.getString(column_index_data); 
     String s[] = fileName.split("/"); 
     mUploadImage.setText(s[s.length - 1]); 
     sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mCapturedImageURI)); 
    } 
} 

什么让画廊crash.Please帮助我。

+2

请将崩溃的堆栈跟踪 - 所有这些,直到最后一个'由...造成'。 – Ridcully

+0

Stacktrace中没有显示任何内容。它说的是“应用程序(进程com.cooliris.media)已意外停止。请再试一次。 – Payal

+0

logcat中始终有一个堆栈跟踪,你知道吗? – njzk2

回答

2

因此,有很多杂耍后,我发现这个解决方案,为我工作..

打开媒体库或摄像头: -

if (!Environment.getExternalStorageState().equals(
       Environment.MEDIA_MOUNTED)) { 
      Toast.makeText(getApplicationContext(), "sdcard not mounted", 
        Toast.LENGTH_SHORT).show(); 
      break; 
     } 
     AlertDialog.Builder photoDialog = new AlertDialog.Builder(this); 
     photoDialog 
       .setTitle("Photo source") 
       .setCancelable(true) 

       .setPositiveButton("Open Gallery", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, 
            int id) { 
           startActivityForResult(
             new Intent(
               Intent.ACTION_PICK, 
               android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI), 
             54); 
          } 
         }) 

       .setNegativeButton("Open Camera", 
         new DialogInterface.OnClickListener() { 

          public void onClick(DialogInterface dialog, 
            int id) { 


           int apiLevel = Integer.parseInt(Build.VERSION.SDK); 
           if(apiLevel < 11) { 
           try{ 
           //String fileName = "temp.jpg"; 

           Calendar c = Calendar.getInstance(); 
           CAPTURE_TITLE = "si_"+c.getTimeInMillis()+".jpg"; 
           File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera", CAPTURE_TITLE); 
           Uri ImageURI = Uri.fromFile(file); 


           Intent intent = new Intent(
             MediaStore.ACTION_IMAGE_CAPTURE); 
           intent.putExtra(MediaStore.EXTRA_OUTPUT, 
             ImageURI ); 

           startActivityForResult(intent, 99); 

           } 
           catch(Exception e) 
           { 
            LogCreator lg = new LogCreator(); 
            lg.writeBarcodeToFile("Image exc1", e.toString()); 
           } 

           } 
           else{ 

            //String fileName = "temp.jpg"; 
            Date date = new Date(); 
            DateFormat df = new SimpleDateFormat("-mm-ss"); 

            String newPicFile = "Bild"+ df.format(date) + ".jpg"; 
            String outPath = "/sdcard/" + newPicFile; 
            File outFile = new File(outPath); 

            ContentValues values = new ContentValues(); 
            values.put(MediaStore.Images.Media.TITLE, 
              outFile.getAbsolutePath()); 
            mCapturedImageURI = getContentResolver() 
              .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                values); 

            Intent intent = new Intent(
              MediaStore.ACTION_IMAGE_CAPTURE); 
            intent.putExtra(MediaStore.EXTRA_OUTPUT, 
              mCapturedImageURI); 

            startActivityForResult(intent, 96); 
           } 
          } 


         }); 
     photoDialog.show(); 

现在onActivityResult()

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 54 && resultCode == RESULT_OK) { 
     if(data !=null){ 
     fileName = getRealPathFromURI(data.getData()); 
     mUploadImage.setText(getStringNameFromRealPath((String) fileName)); 
     } else { 
      Toast.makeText(getApplicationContext(),"Please select another image.", Toast.LENGTH_SHORT).show(); 
     } 
    } 
    else if (requestCode == 99 && resultCode != 0) { 
     try { 
      File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera", CAPTURE_TITLE); 
      fileName = Environment.getExternalStorageDirectory() + "/DCIM/Camera/"+CAPTURE_TITLE; 

      Uri imgUri = Uri.fromFile(file); 
      //new GetThumbnail().execute(imgUri); 
      mUploadImage.setText(CAPTURE_TITLE); 
      sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
        Uri.parse("file://" 
          + Environment.getExternalStorageDirectory()))); 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } 
     else if (requestCode == 96 && resultCode != 0) { 
      String[] projection = { MediaStore.Images.Media.DATA }; 
      Cursor cursor = managedQuery(mCapturedImageURI, projection, null, 
        null, null); 
      int column_index_data = cursor 
        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      fileName = cursor.getString(column_index_data); 
      String s[] = fileName.split("/"); 
      mUploadImage.setText(s[s.length - 1]); 
      sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mCapturedImageURI)); 
     } 






} 

这些是我用来获取路径的方法: -

/* 
* Returns image real path. 
*/ 
private String getRealPathFromURI(final Uri contentUri) { 
    final String[] proj = { MediaStore.Images.Media.DATA }; 
    @SuppressWarnings("deprecation") 
    final Cursor cursor = managedQuery(contentUri, proj, null, null, null); 
    int column_index = cursor 
      .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 

    return cursor.getString(column_index); 
} 

/* 
* Cuts selected file name from real path to show in screen. 
*/ 
private String getStringNameFromRealPath(final String bucketName) { 
    return bucketName.lastIndexOf('/') > 0 ? bucketName 
      .substring(bucketName.lastIndexOf('/') + 1) : bucketName; 
} 

希望这个帮助。