android从SD卡中选择图像

android从SD卡中选择图像,android,android-intent,android-imageview,Android,Android Intent,Android Imageview,我有一个带有默认图像的imageview,我希望当用户单击imageview打开图像选择器,然后他从SD卡中选择他的图像,然后图像在imageview上 这是我的观点 xml 您需要将图像从SD卡加载到位图,然后将位图图像设置为ImageView: Bitmap bmp = BitmapFactory.decodeFile("/path/to/file.png"); iv_image.setImageBitmap(bmp); 如yahya所述,您还可以从SD卡图像文件中创建一个可绘制图像,然后

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

这是我的观点

xml
您需要将图像从SD卡加载到位图,然后将位图图像设置为ImageView:

Bitmap bmp = BitmapFactory.decodeFile("/path/to/file.png");
iv_image.setImageBitmap(bmp);
yahya
所述,您还可以从SD卡图像文件中创建一个可绘制图像,然后将图像设置为可绘制:

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

您还应该确保在您的清单中包含对SD卡的读写权限。

我想这就是您要寻找的

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

希望对您有所帮助

尝试下面的代码

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

}
我希望它能帮助你


谢谢。

或者作为一个可绘图的。。。iv_image.setImageDrawable(Drawable.createFromPath(“/path/to/file.png”);本周的某个时候,我将运行一些测试,看看哪个更有效xDI不知道图像的路径,用户将从SD卡中选择任何图像作为其个人资料图像,那么什么是
路径/到/file.png
?请让我们知道哪一个更有效:)您是否尝试在单击imageview时从图库中选择图像,并且在imageview上再次设置所选图像?这就是你想要的吗?@Grishu是的,正是我需要的,为什么你要为一个重复的问题写一个答案,这个问题已经被100%的用户提问和投票了@是的,是书面回答帮助了我,对不起,我不擅长搜索让我告诉你“搜索的秘密”。。。首先,我心里有个问题。。准备好就此提出有关Stackoverflow的问题。。当你开始写问题的时候。。你必须为此写标题。。制作一个标题。。。现在停止。。。就停在那里。。复制你写的标题并粘贴到谷歌搜索框中。。。。我希望这将有助于你下次。!!什么是
R.string。选择_pic
和什么是
request code
?您的答案帮助打开SD卡选择图像,现在如何将图像设置为my imageview?很抱歉,字符串只是选择器的标题,而requestCode是在多个意图激发activityResult()时使用的,它只是一个区分符。getPath是什么?什么是getPreview?现在添加了我希望不必在activity returns listener上使用的所有内容?或者什么?请注意,一旦选择图像,就会调用ActivityResult方法。在这个方法中,我们检查被触发的活动是否确实是图像库(通常从同一个活动中触发不同的意图,并期望从每个活动中得到结果)。为此,我们使用了前面传递给startActivityForResult()方法的SELECT_图片整数。非常感谢:)很高兴帮助您..什么是
getPaht
,eclipse不会重新编码它
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);
}
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);
}

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