从库中选择图像给我一个错误加载失败

时间:2016-04-30 04:09:30

标签: android

当我试图从画廊中选择图像时,它给出了一个错误,说“加载失败”。在某些手机中它只是那样(就像一个加号)任何人都可以帮我解决这个问题。谢谢你

     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;
    }
}

2 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分: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) {
        }