android从sdcard中选择图片

时间:2013-02-07 06:16:50

标签: android android-intent android-imageview

我有一个带有默认图像的imageview,我希望当用户点击图像视图打开图像选择器然后他从SD卡中选择他的图像然后图像就在图像视图上,

这是我的imageview

xml

<ImageView
            android:id="@+id/ivImage"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:layout_marginLeft="10dip"
            android:contentDescription="@string/iv_undefinedImage"
            android:src="@drawable/undefinedimage" />

Java

ImageView iv ;
iv_image = (ImageView)findViewById(R.id.iv_signup_image);
        iv_image.setOnClickListener(this);
public void onClick(View v) {
switch (v.getId()) {
        case R.id.iv_signup_image:
break;
}

4 个答案:

答案 0 :(得分:3)

我认为这就是你要找的东西

if (Environment.getExternalStorageState().equals("mounted")) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_PICK);
    startActivityForResult(
        Intent.createChooser(
            intent,
            "Select Picture:"),
        requestCode);
}

并处理回调

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri selectedImageUri = data.getData();
    String selectedImagePath = getPath(selectedImageUri);
    Bitmap photo = getPreview(selectedImagePath);
}


public String getPath(Uri uri) {
    String res = null;
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = getActivity().getContentResolver().query(uri, proj, null, null, null);
    if(cursor.moveToFirst()){;
        int column_index = cursor.getColumnIndexOrThrow(proj[0]);
        res = cursor.getString(column_index);
    }
    cursor.close();
    return res;
}

public Bitmap getPreview(String fileName) {
    File image = new File(fileName);

    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(image.getPath(), bounds);
    if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) {
        return null;
    }
    int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
        : bounds.outWidth;
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = originalSize / 64;
    return BitmapFactory.decodeFile(image.getPath(), opts);
}

希望有所帮助

答案 1 :(得分:1)

您需要将SDCard中的图像加载到位图,然后将位图图像设置为ImageView:

Bitmap bmp = BitmapFactory.decodeFile("/path/to/file.png");
iv_image.setImageBitmap(bmp);

yahya所述,您还可以从SDCard图像文件中创建一个drawable,然后将图像设置为drawable:

iv_image.setImageDrawable(Drawable.createFromPath("/path/to/file.png"));

您还应确保在您的清单中包含读取(/写入)SDCard的权限。

答案 2 :(得分:1)

试试下面的代码。

public class MainActivity extends Activity {
ImageView iv_image,img1;
int column_index;
  Intent intent=null;
// Declare our Views, so we can access them later
String logo,imagePath,Logo;
Cursor cursor;
//YOU CAN EDIT THIS TO WHATEVER YOU WANT
private static final int SELECT_PICTURE = 1;

 String selectedImagePath;
//ADDED
 String filemanagerstring;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    img1= (ImageView)findViewById(R.id.image1);
    iv_image= (ImageView)findViewById(R.id.iv_signup_image);

    img1.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

            // in onCreate or any event where your want the user to
            // select a file
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,
                    "Select Picture"), SELECT_PICTURE);
       }
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();

            //OI FILE Manager
            filemanagerstring = selectedImageUri.getPath();

            //MEDIA GALLERY
            selectedImagePath = getPath(selectedImageUri);


            img.setImageURI(selectedImageUri);

           imagePath.getBytes();
           TextView txt = (TextView)findViewById(R.id.title);
           txt.setText(imagePath.toString());

           Bitmap bm = BitmapFactory.decodeFile(imagePath);
           iv_image.setImageBitmap(bm);



        }

    }

}

//UPDATED!
public String getPath(Uri uri) {
String[] projection = { MediaColumns.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
column_index = cursor
        .getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
 imagePath = cursor.getString(column_index);

return cursor.getString(column_index);
}

}

我希望它会对你有所帮助。

感谢。

答案 3 :(得分:1)

private final int GET_USER_IMAGE_FROM_GALLERY = 10;

ImageView imageView = findViewById(R.id.ivImage);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
        "Select Picture"),
GET_USER_IMAGE_FROM_GALLERY);
});


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == GET_USER_IMAGE_FROM_GALLERY) {

        if (data != null) {

            Uri selectedImageUri = data.getData();
            String selectedImagePath = getPath(selectedImageUri);

            try {
                File imageFile = new File(selectedImagePath);
                Bitmap bitmap = BitmapFactory.decodeFile(imageFile
                        .getAbsolutePath());

                imageView.setImageBitmap(bitmap);
            } catch (Exception e) {
            }

        }
}

private String getPath(Uri selectedImageUri) {

    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(selectedImageUri,
            projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}