如何在android中从SD卡获取图像?

如何在android中从SD卡获取图像?,android,imageview,Android,Imageview,我想创建一个简单的应用程序,在其中我想从SD卡中获取图像,但无法成功。 我的代码在下面 File myFile = new File("/sdcard/photo.jpg"); ImageView jpgView = (ImageView)findViewById(R.id.imageView); BitmapDrawable d = new BitmapDrawable(getResources(), myFile.getAbsolutePath()); jpgView.setImageDra

我想创建一个简单的应用程序,在其中我想从SD卡中获取图像,但无法成功。 我的代码在下面

File myFile = new File("/sdcard/photo.jpg");
ImageView jpgView = (ImageView)findViewById(R.id.imageView);
BitmapDrawable d = new BitmapDrawable(getResources(), myFile.getAbsolutePath());
jpgView.setImageDrawable(d);
但是无法获取图像。

试试这个

Bitmap bitmap = BitmapFactory.decodeFile(myFile.getAbsolutePath());
jpgView.setImageBitmap(bitmap);

使用下面的代码而不是您的代码

File f = new File("/mnt/sdcard/photo.jpg");
ImageView mImgView1 = (ImageView)findViewById(R.id.imageView);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
mImgView1.setImageBitmap(bmp);

我想有一个更简单的方法肯定会对你有所帮助

Java版本:

import static android.app.Activity.RESULT_OK;

private Bitmap bitmap;
private Uri filePath;

private void showFileChooser() {
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, GET_IMAGE);
}

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

    if (requestCode == GET_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {

        filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
            imageView1.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
import android.app.Activity.RESULT_OK

private val GET_IMAGE = 1
private lateinit var filePath : Uri
private lateinit var bitmap : Bitmap

private fun selectImage() {
    var photoPickerIntent : Intent = Intent(Intent.ACTION_PICK)
    photoPickerIntent.setType("image/*")
    startActivityForResult(photoPickerIntent, GET_IMAGE)
}


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if(requestCode == GET_IMAGE && resultCode == RESULT_OK && data != null){
        filePath = data.data

        try{
            bitmap = MediaStore.Images.Media.getBitmap(activity!!.contentResolver, filePath)
            imageView!!.setImageBitmap(bitmap)
        }catch (exception : IOException){
            Toast.makeText(activity,"Error Loading Image!!!", Toast.LENGTH_SHORT).show()
        }
    }
}
Kotlin版本:

import static android.app.Activity.RESULT_OK;

private Bitmap bitmap;
private Uri filePath;

private void showFileChooser() {
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, GET_IMAGE);
}

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

    if (requestCode == GET_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {

        filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
            imageView1.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
import android.app.Activity.RESULT_OK

private val GET_IMAGE = 1
private lateinit var filePath : Uri
private lateinit var bitmap : Bitmap

private fun selectImage() {
    var photoPickerIntent : Intent = Intent(Intent.ACTION_PICK)
    photoPickerIntent.setType("image/*")
    startActivityForResult(photoPickerIntent, GET_IMAGE)
}


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if(requestCode == GET_IMAGE && resultCode == RESULT_OK && data != null){
        filePath = data.data

        try{
            bitmap = MediaStore.Images.Media.getBitmap(activity!!.contentResolver, filePath)
            imageView!!.setImageBitmap(bitmap)
        }catch (exception : IOException){
            Toast.makeText(activity,"Error Loading Image!!!", Toast.LENGTH_SHORT).show()
        }
    }
}

您必须使用jpgView.setImageBitmap(d),而不是jpgView.setImageBitmap(d)。对于目录,我可以知道必须是mnt吗