ImageView不改变图像来自Gallery

时间:2016-01-13 15:19:51

标签: android android-studio image-gallery

我正在使用此代码从图库中获取图像或拍照 拍照效果很好。
从相机拍摄的照片将显示在imageview上,
但与从画廊拍摄的图像不同,它总是空白。

 public void fromCamera(int id) {
    Intent intent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, IMAGE_CAPTURE);
}

public void fromGallery(int id) {
    Intent intent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intent.setType("image/*");
    startActivityForResult(Intent.createChooser(intent, "Pick a Picture"),
            IMAGE_PICK);
}

public String getRealPathFromURI(Uri contentUri) {
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } catch (Exception e) {
        return contentUri.getPath();
    }
}

private void imageFromCamera(int resultCode, Intent data) {
    this.mButtonCarPhoto.setImageBitmap((Bitmap) data.getExtras().get("data"));
    Uri selectedImageUri = data.getData();
    mSelectedImagePath = getRealPathFromURI(selectedImageUri);
    Log.e("Camera",mSelectedImagePath);
    // mBase64Image = CONVERT_IMG_BASE64(mSelectedImagePath);
}

private void imageFromGallery(int resultCode, Intent data) {
    Uri selectedImage = data.getData();
    String[] filePathColumn = { MediaStore.Images.Media.DATA };

    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
    assert cursor != null;
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    mSelectedImagePath = cursor.getString(columnIndex);
    Log.e("Gallery",mSelectedImagePath);
    cursor.close();
    this.mButtonCarPhoto.setImageBitmap(BitmapFactory.decodeFile(mSelectedImagePath));
    //mBase64Image = CONVERT_IMG_BASE64(mSelectedImagePath);*/
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        switch (requestCode) {
            case IMAGE_PICK:
                this.imageFromGallery(resultCode, data);
                break;
            case IMAGE_CAPTURE:
                this.imageFromCamera(resultCode, data);
                break;
            default:
                break;
        }
    }
}  

我做错了什么?

2 个答案:

答案 0 :(得分:0)

为什么会这样?

intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Pick a Picture"), IMAGE_PICK);

如果您只想从图库中选择,请从代码中删除theze行。

答案 1 :(得分:0)

试试这个,它对我来说很完美:

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, 1);

然后覆盖onActivityResult方法:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode==1) {  // from gallery
        if (data != null) {
            Uri photoUri = data.getData();
            if (photoUri != null) {
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                path = cursor.getString(columnIndex); // path is a static variable
                cursor.close();
                new GetImages().execute();
            }
        }
    }
}

这是内部类GetImages,用于更新异步任务中的图像视图:

 public class GetImages extends AsyncTask<Void, Void, JSONObject>
{
    @Override
    protected JSONObject doInBackground(Void... params)
    {
        Bitmap bitmap = BitmapFactory.decodeFile(path.toString().trim());
        if (bitmap != null) {
            bitmap = Bitmap.createScaledBitmap(bitmap, 500, 500, true);
            currentBitmap = bitmap; // current Bitmap is a static variable
            drawable = new BitmapDrawable(bitmap); // drawable is a static variable
            // salvataggio foto in locale
            db.open();
            String bitmapString = BitMapToString(bitmap);
            db.updateImage(bitmapString, userName);
            db.close();
            JSONObject json = savePhotoInRemote(bitmapString);
            return json;
        }
        return null;
    }

    protected void onPostExecute(JSONObject result)
    {
        imageview_photo.setBackground(drawable); // QUI AGGIORNI LA TUA IMAGE VIEW
    }
}