2016-04-30 50 views
-1

当我试图从图库中选择图像时,它给我一个错误说加载失败。在一些手机只有它像这样(就像一个加)可以任何身体帮我解决这个issue.Thank你从图库中选择图像给我一个错误加载失败

 private void openGalleryForImageSelection() 
{ 
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    try 
    { 
     startActivityForResult(intent, IMAGE_FROM_GALLERY); 
    } 
    catch(Throwable e) 
    { 
     Toast.makeText(this,getResources().getString(R.string.image_error),Toast.LENGTH_SHORT).show(); 
    } 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if (((requestCode == IMAGE_FROM_GALLERY) || (requestCode == IMAGE_FROM_CAMERA)) && (Activity.RESULT_OK == resultCode)) 
    { 
     if (requestCode == IMAGE_FROM_GALLERY) 
     { 
      mImageCaptureUri = data.getData(); 
     } 
     cropTheImage(); 
     return; 
    } 
} 
+0

所以,哪里是你的代码? – ianhanniballake

+0

谢谢你的回应。我已经添加了我的代码。你可以请检查并建议我一个解决方案来解决这个问题! –

+0

其中是cropTheImage()函数 –

回答

0
void selectImage() {  
Intent intent = new Intent(
    Intent.ACTION_PICK, 
    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    intent.setType("image/*"); 
    startActivityForResult(
    Intent.createChooser(intent, "Select File"), 
    IMAGE_FROM_GALLERY); 
} 



protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == IMAGE_FROM_GALLERY && resultCode == RESULT_OK) { 
     Uri selectedImageUri = data.getData(); 
     String[] projection = { MediaColumns.DATA }; 
     CursorLoader cursorLoader = new CursorLoader(this,selectedImageUri, projection, null, null, 
     null); 
     Cursor cursor =cursorLoader.loadInBackground(); 
     int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA); 
     cursor.moveToFirst(); 
     String selectedImagePath = cursor.getString(column_index); 
     Bitmap bm; 

     file = new File(selectedImagePath); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(selectedImagePath, options); 
     final int REQUIRED_SIZE = 200; 
     int scale = 1; 
     while (options.outWidth/scale/2 >= REQUIRED_SIZE 
     && options.outHeight/scale/2 >= REQUIRED_SIZE) 
     scale *= 2; 
     options.inSampleSize = scale; 
     options.inJustDecodeBounds = false; 
     bm = BitmapFactory.decodeFile(selectedImagePath, options); 
    } 
} 
0
private void openGallery() { 
    Intent intent = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), 
      PICK_IMAGE); 
} 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == PICK_IMAGE) { 

     try { 
      taken = "gallery"; 
      selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

      cursor = getActivity().getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      cursor.moveToFirst(); 
      columnindex = cursor.getColumnIndex(filePathColumn[0]); 
      file_path = cursor.getString(columnindex); 
      // Log.e("Attachment Path:", attachmentFile); 
      // tv_attach.setText(file_path); 
      URI = Uri.parse("file://" + file_path); 
      image_path = file_path; 
      cursor.close(); 
      if (resultCode == 0) { 
       dialog_camera.dismiss(); 
      } else { 
       dialog_camera.dismiss();  
      } 
     } catch (Exception e) { 
     } 
+0

您能否提一下这些更改以及为什么需要这些更改? –

相关问题