Android 我的形象可以';无法加载到我的ImageView中。出了什么问题?

Android 我的形象可以';无法加载到我的ImageView中。出了什么问题?,android,image,imageview,android-imageview,Android,Image,Imageview,Android Imageview,我正在尝试将特定图像从我的目录加载到我的应用程序的ImageView中,但它不起作用。整个事情变得一片空白。有人能看看我的代码哪里出错了吗?谢谢 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_viewer); ImageView imageView = (Imag

我正在尝试将特定图像从我的目录加载到我的应用程序的ImageView中,但它不起作用。整个事情变得一片空白。有人能看看我的代码哪里出错了吗?谢谢

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_viewer);

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

    try {
       //this toast below appears
        Toast.makeText(getApplicationContext(), "try",  Toast.LENGTH_LONG).show(); 

       //below is the path for the image
        Uri uri = Uri.parse("file://sdcard/myFoodDiary/snaps/default.jpg");

        System.out.println("Image URI: " + uri.toString());
        imageView.setImageURI(uri);

    } catch (Exception e) {
        // TODO: handle exception
        Toast.makeText(getApplicationContext(), "oops", Toast.LENGTH_LONG).show();
    }

}

我认为这不是一条有效的路径:

Uri.parse("file://sdcard/myFoodDiary/snaps/default.jpg");
试一试

但我最好做点像这样的事情

File file = new File (Environment.getExternalStorageDirectory() + "/myFoodDiary/snaps/default.jpg");
ImageView imageView = (ImageView) findViewById(R.id.iv);
imageView.setImageDrawable(Drawable.createFromPath(file.getAbsolutePath()));

对于任何感兴趣的人,请按照Artoo Detoo的说明更正代码:

File file = new File (Environment.getExternalStorageDirectory() + "/myFoodDiary/snaps/default.jpg");
ImageView imageView = (ImageView) findViewById(R.id.icon);
imageView.setImageDrawable(Drawable.createFromPath(file.getAbsolutePath()));

将图像放在其他地方——您以前成功访问过的地方——看看它是否有效。无论结果如何——如果它起作用或不起作用——你都会收集到一些信息。你添加了阅读SD卡的权限吗?@Obversity,应该可以,尽管我从未尝试过在其他地方访问它。@HamidShatu,是的,权限已经在清单中了。在myFoodDiary前面漏了一个“/”,但在其他方面很棒!谢谢
File file = new File (Environment.getExternalStorageDirectory() + "/myFoodDiary/snaps/default.jpg");
ImageView imageView = (ImageView) findViewById(R.id.icon);
imageView.setImageDrawable(Drawable.createFromPath(file.getAbsolutePath()));