【问题标题】:Unable to load image in a ImageView from the Internal Storage无法从内部存储加载 ImageView 中的图像
【发布时间】:2016-01-26 02:55:37
【问题描述】:

我正在尝试从内部存储器添加图像。

我正在尝试使用此代码

        ImageView message_image=(ImageView)v.findViewById(R.id.message_image);
        File imgFile = new File(img_db);
        Log.e("img_db",img_db+"------------->");
        if(imgFile.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            message_image.setImageBitmap(myBitmap);
         }

这里的 img_db 是我从我的 Sqlite 表中获取的路径。当我记录它时,我得到了这样的路径

E/img_db: file:///storage/sdcard0/download/img11024FILE.jpg------------->

我已经在我的清单中授予了这个权限

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

为了下载图像并保存到我正在使用此代码的手机

  public String downloadFile(String url_i_) {

    try {


        URL url
                = new URL(url_i_);
        Log.e("url_fetch--->img", String.valueOf(url));

        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);

        // connect
        urlConnection.connect();

        // set the path where we want to save the file
        SDCardRoot = Environment.getExternalStorageDirectory();
        // create a new file, to save the downloaded file
        rand = new External_function().getnRandom();
        file = new File(SDCardRoot, "/download/img" + rand+".jpg");
        FileOutputStream fileOutput = new FileOutputStream(file);
        InputStream inputStream = urlConnection.getInputStream();
        // this is the total size of the file which we are downloading
        totalSize = urlConnection.getContentLength();



        // create a buffer...
        byte[] buffer = new byte[1024];
        int bufferLength = 0;

        while ((bufferLength = inputStream.read(buffer)) > 0) {
            fileOutput.write(buffer, 0, bufferLength);
            downloadedSize += bufferLength;
            // update the progressbar //


                    float per = ((float) downloadedSize / totalSize) * 100;
                    /*cur_val.setText("Downloading.... " + downloadedSize
                            + "KB / " + totalSize + "KB (" + (int) per
                            + "%)");*/
                    String i = "Downloading.... " + downloadedSize
                            + "KB / " + totalSize + "KB (" + (int) per
                            + "%)";


        }

        fileOutput.close();




        file = new File(Environment.getExternalStorageDirectory() + "/download/img" + rand+".jpg"); // set your audio path
                file_path= String.valueOf(Uri.fromFile(file));





    }  catch (final Exception e) {
        Log.e("////////////////file", String.valueOf(e));
    }
    return file_path;
}

在我的服务类中,当我得到图像路径作为回报时,我将它存储在我的 sqlite 表中,就像这样

   String img = socketOperator.downloadFile(event_img);
                localstoragehandler.insert(id, msg_user_by, msg_read_by, msg_content, msg_grp_id, ms‌​g_sent_time,type,event_time,event_lat,event_log,event_name,event_address,img); 

我检查了带有图像和 id 的路径是否相同。我尝试了这个,但真的不知道我做错了什么。

【问题讨论】:

  • 您还没有告诉我们到底是什么问题。你这么牛逼? ImageView 中没有任何内容吗?如果没有这些信息,就很难为您提供帮助。
  • @Xaver 感谢您的回复,实际上我的图像没有加载到图像视图中
  • 但乍一看:您说您的清单中已经拥有WRITE_EXTERNAL_STORAGE 权限。但是你也有READ_EXTERNAL_STORAGE 权限吗?你知道...既然你试图阅读而不是写作?
  • @XaverKapeller 我添加了 READ_EXTERNAL_STORAGE 权限,但问题没有解决

标签: android android-sqlite android-imageview android-bitmap internal-storage


【解决方案1】:

您需要此权限:

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

读取_EXTERNAL_STORAGE不是WRITE_EXTERNAL_STORAGE

【讨论】:

    【解决方案2】:

    @CommonsWare 已经回答了这个问题。尝试改变获得路径的方式。 (Original Answer)

    为什么会这样?

    因为这些年来外部存储的路径发生了变化,这就是为什么您一开始就不应该对路径进行硬编码。

    如何以更“正式和正确”的方式获取路径目录 而不是硬编码路径?

    使用 Environment.getExternalStorageDirectory()。

    在您的测试环境中,/sdcard 是指向 /storage/sdcard0 的符号链接或硬链接,由设备制造商设置。但是,不能保证所有设备都会有这样的 /sdcard 符号链接。


    如果这对您没有帮助。这肯定会:Reading from internal storage.

    更新

    请查看此“/download/img”的所有用法...

    你缺少一个“/”...用“/download/img/”替换它

    【讨论】:

    • 感谢您的回复。但我正在使用这个文件 = new File(Environment.getExternalStorageDirectory() + "/download/img" + rand+".jpg"); file_path= String.valueOf(Uri.fromFile(file));这个文件路径我存储在 Sqlite 表中
    • 您能添加其余的代码吗?您在哪里指定“img_db”
    • 是的,你是 coreect,但是当我尝试这个时,我得到了异常 E/img_db: file:///storage/sdcard0/download/img15512FILE.jpg----------- --> 01-25 14:30:38.522 2509-2509/com.myapp.cowite E/BitmapFactory: 无法解码流: java.io.FileNotFoundException: /file:/storage/sdcard0/download/img15512FILE.jpg: 打开失败:ENOENT(没有这样的文件或目录)
    • 看日志,还是少了一个正斜杠:"download/img/15512FILE.jpg"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多