2016-06-10 107 views
0

从画廊中选择图片或从相机拍摄照片(在我的情况下,它返回byte []数组到我的应用程序)后,有人面临的问题?android-ANR从画廊/相机拍摄图片

如何解决?

Upd。这是代码如何挑选图像的代码。但是这部分代码没有改变,前一段时间它的工作(代码的其他部分发生了变化,我不知道它是否重要)。

void chooseOrTakePhotoDialog(OnActivityResultListener listener) { 
    onActivityResultListener = listener; 
    final CharSequence[] items = { "Take Photo", "Choose from Library", "Cancel" }; 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Add Photo"); 
    builder.setItems(items, new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int item) { 
      if (items[item].equals("Take Photo")) { 
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(intent, REQUEST_CAMERA); 
      } else if (items[item].equals("Choose from Library")) { 
       Intent intent = new Intent(
         Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       intent.setType("image/*"); 
       startActivityForResult(
         Intent.createChooser(intent, "Select File"), 
         SELECT_FILE); 
      } else if (items[item].equals("Cancel")) { 
       dialog.dismiss(); 
      } 
     } 
    }); 
    builder.show(); 
} 

interface OnActivityResultListener { 
    void onPhotoByteArray(byte[] bytes); 
} 
private OnActivityResultListener onActivityResultListener; 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    Log.wtf("onActivityResult", "req="+requestCode+" res="+resultCode+" data="+data); 
    if (resultCode != RESULT_OK) return; 
    switch (requestCode) { 
     case REQUEST_CAMERA: 
      if (onActivityResultListener != null) { 
       Bitmap bmp = (Bitmap) data.getExtras().get("data"); 
       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
       bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
       onActivityResultListener.onPhotoByteArray(stream.toByteArray()); 
      } 
      break; 
     case SELECT_FILE: 
      Uri uri = data.getData(); 
      try { 
       byte[] bytes = Helper.readBytes(getContentResolver().openInputStream(uri)); 
       onActivityResultListener.onPhotoByteArray(bytes); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      break; 
    } 
} 
+1

请发布您的代码和错误。 – earthw0rmjim

+0

它很奇怪,但在Logs中没有错误...它只是冻结然后关闭(但前一段时间它的工作,与选择相同的代码;我改变了一些其他的东西) –

+0

你为什么要返回字节数组?我可以知道原因吗? – SripadRaj

回答

0

对于SELECT_FILE的情况下,可以尝试让Bitmap像下面。

case SELECT_FILE: 
      if (data != null) { 
       try { 
        Uri selectedImage = data.getData(); 
        String[] filePath = {MediaStore.Images.Media.DATA}; 
        Cursor c = getApplicationContext().getContentResolver().query(
          selectedImage, filePath, null, null, null); 
        c.moveToFirst(); 
        int columnIndex = c.getColumnIndex(filePath[0]); 
        String picturePath = c.getString(columnIndex); 
        c.close(); 
        Bitmap thumbnail = BitmapFactory.decodeFile(picturePath); 

       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       break; 
+0

Thanx!所以它比转换为byte []更快? –

+0

在'onActivityResult()'中转换会消耗时间或导致ANR。这就是为什么我建议你在将它发送到服务器之前首先获取位图,将其转换为字节数组,然后将其发送到服务器。 – SripadRaj

+0

以及在'onActivityResult()'或更高版本中执行它有什么区别? –