鉴于realpath将其显示为ImageView。 (使用Glide库)

时间:2017-05-17 01:03:12

标签: android android-recyclerview android-glide

我试图用Glide图书馆做这件事,但它不起作用,而且我不知道该怎么做。

当我使用此Intent时,Glide正常工作并加载所选图像。

Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery,PICK_PHOTO);

我在这里加载图片。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==PICK_PHOTO && resultCode == RESULT_OK){

         Glide.with(this).load(data.getData()).into(imageView);
}

上面的代码运行正常。但是,当我给出真实路径时,没有任何事情发生。

这是我的图片路径。

imagePath="/storage/emulated/0/Pictures/IMG-20161029-WA0025.jpg"

我使用这个非常简单的代码,但它不起作用。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView imageView = (ImageView)findViewById(R.id.photo);
    File imgFile = new  File("/storage/emulated/0/Pictures/IMG-20161029-WA0025.jpg");
    Glide.with(this).load(imgFile).into(imageView);

}

我已尝试使用Bitmaps。

File imgFile = new  File("/storage/emulated/0/Pictures/IMG-20161029-WA0025.jpg");
if(imgFile.exists()){
    Bitmap bitmap = BitmapFactory.decodeFile(imgFile);
    imageView.setImageBitmap(bitmap);
    //Glide.with(this).load(imgFile).into(imageView);
}

但它也不起作用。

PD:我的最终项目是将一些先前存储在SD卡中的图像放入RecyclerView中。但我想弄清楚如何首先给出真实路径的图像。

谢谢!

修改

这是我的清单。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.melquiadesrodriguez.showimage">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我这样做,但似乎不可行。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView imageView = (ImageView)findViewById(R.id.photo);
    File imgFile = new  File(getFilename());
    Glide.with(this).load(imgFile).into(imageView);

}

private String getFilename() {
        String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
        File file = new File(filepath + "/Pictures/IMG-20161029-WA0025.jpg" );
        if (!file.exists()) {
            return null;
        }
        return file.getAbsolutePath();
    }

1 个答案:

答案 0 :(得分:0)

你需要从来自activityresult的uri给出真正的路径

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==PICK_PHOTO && resultCode == RESULT_OK){
         Uri selectedImage = data.getData();

         String selectedImagePath = getRealPathFromURI(selectedImage);
         File imageUri = new File(selectedImagePath);

         Glide.with(this).load(imageUri).into(imageView);
    }
 }


private String getRealPathFromURI(Uri contentURI) {
    String path;
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) {
        path = contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        path =  cursor.getString(index);
    }
    assert cursor != null;
    cursor.close();

    return path;
}

修改

您似乎想要访问外部SD卡

确保您有权在清单中读取或写入

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

试试这个..

private String getFilename() {
    String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
    File file = new File(filepath + "/Pictures/IMG-20161029-WA0025.jpg" );
    if (!file.exists()) {
        return null;
    }
    return file.getAbsolutePath();
}

修改

哦,在此之前,我意识到,有一些设备有不同的外部文件存储路径..所以基本上getExternalStorageDirectory()将返回/storage/sdcard0,所以你可能需要检查外部存储路径的存在< / p>

String filename; 

if(new File("/storage/extSdCard/").exists()) { 
  filename="/storage/extSdCard/";
} 
else if(new File("/storage/emulated/0/").exists(){
  filename = "/storage/emulated/0/";
}
else if(new File("/storage/sdcard1/").exists()){ 
  filename="/storage/sdcard1/";
} 
else if(new File("/storage/usbcard1/").exists()) { 
  filename="/storage/usbcard1/";
} 
else if(new File("/storage/sdcard0/").exists()) { 
  filename="/storage/sdcard0/";
}
else if(new File("/mnt/sdcard/").exists()){
  filename = "/mnt/sdcard/";
}

File imgFile = new  File(filename+"Pictures/IMG-20161029-WA0025.jpg");

if(imgFile.exists()){
  //set image
}