我想在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);
    }
}

最佳答案

测试此代码:

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

10-06 03:30