简体   繁体   English

滑行不加载图像从SD卡

[英]glide not loading image from sdcard

I want to put an image that is in my sdcard but it doesn't show up, i thought there is a problem with the library of glide or a permission, i'm stacked in there for 2 days please help me. 我想在sdcard中放一个图像,但它没有显示,我认为滑行库或权限存在问题,我在其中堆放了2天,请帮助我。

public class MainActivity extends AppCompatActivity {

    public final static String PATH_SD_CARD = "/Images/Mj.jpg";
    private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;



    @TargetApi(Build.VERSION_CODES.M)
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                != getPackageManager().PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (shouldShowRequestPermissionRationale(
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                // Explain to the user why we need to read the contacts
            }

            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

            // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
            // app-defined int constant that should be quite unique

            return;
        }

        ImageView img = findViewById(R.id.imageView);

        Glide.with(this)
                .load(Environment.getExternalStorageDirectory().getAbsolutePath()+PATH_SD_CARD)
                .into(img);
    }
}

have you added permission in android manifest file like below code.. 您是否在Android清单文件中添加了权限,如以下代码所示。

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

Calling Environment.getExternalStorageDirectory() doesn't guarantee extеrnal storage in the way of SDcard, but word external is in reference to "external to the Application". 调用Environment.getExternalStorageDirectory()并不能保证extеrnal存储在SD卡的方式,但字是外部参照“外部应用程序”。 This path will in most cases give you: /storage/emulated/0/ . 在大多数情况下,该路径将为您提供: /storage/emulated/0/

Since path to the any "drive" that isn't integral part of the device can have different names, you need some other way to find it, regardless of the device. 由于指向不是设备组成部分的任何“驱动器”的路径可以具有不同的名称,因此,无论设备如何,都需要使用其他方法来查找它。 IMO, the most efficient and secure way is querying the MediaStore with some criteria. IMO,最有效和安全的方法是使用某些条件查询MediaStore

Quick solution could be to use getExternalFilesDirs() witch gives you Files[] where your App hat "its" directories. 快速解决方案可能是使用getExternalFilesDirs()巫婆为您提供Files[] ,其中您的应用程序包含“其”目录。 Second element to this array is path on the SDCard. 该阵列的第二个元素是SDCard上的路径。 If you go two "levels" up from there ( .getParentFile() ), you'll get a reference to your "root" SDCard directory. 如果从那里向上两个“级别”( .getParentFile() ), .getParentFile()获得对“根” SDCard目录的引用。

And off course, as already mentioned, do use debugging extensively to get an idea where it went wrong. 当然,正如已经提到的,请确实广泛使用调试来弄清哪里出错了。

Test this code : 测试此代码:

String fileName = "myImage.jpg";
        String imagePath = Environment.getExternalStorageDirectory() + "/" + fileName;

        File file = new File(imagePath);
        Uri imageUri = Uri.fromFile(file);

        Glide.with(this)
                .load(imageUri)
                        .into(imgView);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM