Java 无法在imageview中显示图像

Java 无法在imageview中显示图像,java,android,imageview,Java,Android,Imageview,我正在尝试在imageview中显示图像 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView = (ImageView)findViewById(R.id.imageView1); try { File

我正在尝试在imageview中显示图像

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

    ImageView imageView = (ImageView)findViewById(R.id.imageView1);
    try {
        File imgFile = new File("C:\\photos\\a.jpg");
        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                    if (myBitmap == null) {
            Log.e("BITMAP", "myBitmap is NULL");
        }
        imageView.setImageBitmap(myBitmap);         
    }catch (Exception e) {
        e.printStackTrace();
    }
}
我可以看到错误消息“myBitmap为空”。 我需要做些什么来确保路径被识别

谢谢

路径
“C:\\photos\\a.jpg”
引用了Windows系统中的路径,这是无效的。Android对Windows一无所知

您需要将文件放在资源目录或资产目录中,并提供相应的路径。

尝试以下操作: 将图像放入
资产
文件夹,然后:

private Bitmap getBitmapFromAsset(String strName)
{
    AssetManager assetManager = getAssets();
    InputStream istr = null;
    try {
        istr = assetManager.open(strName);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(istr);
    return bitmap;
}
然后将
imageView
中的图像位图设置为:

imageView.setImageBitmap(getBitmapFromAsset("fileName")); 
或者,如果图像文件位于SD卡上,请尝试以下操作:

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

希望这对您有所帮助。

记录路径并将图像放在手机存储器或SD卡下,然后获取图像路径。也不要对路径进行编码查看我的答案并告诉你的结果。。。