从图库中选择照片

时间:2015-01-05 10:43:42

标签: android

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, 2);
}

protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    Uri selectedImage = intent.getData();
    Bitmap image = BitmapFactory.decodeFile(selectedImage.toString());
    ImageView image1= (ImageView) findViewById(R.id.imageView);
    image1.setImageBitmap(image);
    String ABC ="ABC";
}

我得到Bitmap图像的空值。我该如何更改代码以便获取值?

5 个答案:

答案 0 :(得分:1)

试试这个

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ImageView;


public class MainActivity extends Activity {
    ImageView image1;
    private static final int SELECT_PICTURE = 1;
    private String selectedImagePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image1 = (ImageView) findViewById(R.id.imageView);
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, ""), 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if (data.getData() != null) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                image1.setImageURI(selectedImageUri);
            }

        } else
            super.onActivityResult(requestCode, resultCode, data);
    }

    private String getPath(Uri selectedImageUri) {
        String[] projection = {MediaStore.MediaColumns.DATA};
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }
}

答案 1 :(得分:0)

无需将Uri转换为位图。直接调用以下方法将图像设置为图像视图。

image1.setImageURI(selectedImage);

修改

你可以像这样从uri获取位图。

Bitmap image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);

答案 2 :(得分:0)

使用此代码从图库中获取图片。

 Uri selectedImage = data.getData();
 String[] filePath = { MediaStore.Images.Media.DATA };
 Cursor c = getContentResolver().query(selectedImage, filePath,
                    null, null, null);
 c.moveToFirst();
 int columnIndex = c.getColumnIndex(filePath[0]);
 picturePath = c.getString(columnIndex);
 c.close();
 setPic(picturePath);


private void setPic(final String path) {
    int targetW = imageview.getWidth();
    int targetH = imageview.getHeight();

    final BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    bitmap = BitmapFactory.decodeFile(path, bmOptions);
    imageview.setImageBitmap(bitmap);
  }

答案 3 :(得分:0)

试试这个......

   protected void onActivityResult(int requestCode, int resultCode, Intent intent){
  if (requestCode == 2) {
        Uri selectedImage = intent.getData();
        String fileUriString = selectedImage .toString().replaceFirst("file:///", "/").trim();
        Bitmap image = BitmapFactory.decodeFile(selectedImage.toString());
        ImageView image1= (ImageView) findViewById(R.id.imageView);
        image1.setImageBitmap(image);
        String ABC ="ABC";
}
    }

答案 4 :(得分:0)

尝试以下代码,这对我来说非常好:

打开图库并选择图片:

Intent g = new Intent(Intent.ACTION_GET_CONTENT);
g.setType("image/*");
startActivityForResult(g, FROM_GALLERY);

FROM_GALLERY 定义为int。

获取图像并将其设置为imageView:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);

     if (requestCode == FROM_GALLERY resultCode == RESULT_OK && null != data) {
         Uri selectedImage = data.getData();
         String[] filePathColumn = { MediaStore.Images.Media.DATA };
         Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);

         cursor.moveToFirst();
         int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
         String picturePath = cursor.getString(columnIndex);
         cursor.close();

         ImageView image1 = (ImageView) findViewById(R.id.imageView);
         image1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
  }
}

希望这可以帮助你!!!