0

I am trying to add the Image from the the Internal Memory.

I am trying to use this code for that

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

here img_db is my path that i am getting from my Sqlite table.when i log it i get the fath like this

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

I have already given this permission in my manifest

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

For downloading image and save to the phone i am using this code

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

And inside my service class when i get the image path in return then i store it inside my sqlite table like this

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

I checked the path with image and id are same.I try this but really don't know what I am doing wrong.

Shubhank Gupta
  • 833
  • 2
  • 15
  • 35

2 Answers2

1

You need this permission:

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

READ_EXTERNAL_STORAGE not WRITE_EXTERNAL_STORAGE

Barett
  • 5,826
  • 6
  • 51
  • 55
Lucas
  • 431
  • 3
  • 7
0

@CommonsWare has already answered this. Try changing how you get your path. (Original Answer)

Why would this happen?

Because the path to external storage has changed over the years, which is why you should have never hard-coded the path in the first place.

how can I get the path directory in a more "formal and right" way instead of hardcoding the path?

Use Environment.getExternalStorageDirectory().

On your test environment, /sdcard is a symlink or hardlink to /storage/sdcard0, as set up by the device manufacturer. However, there is no guarantee that all devices will have such an /sdcard symlink.


If that does not help you. This definitely will: Reading from internal storage.

UPDATE

Please look for all usages of this "/download/img" ...

You are missing a "/" ... Replace it with "/download/img/"

Community
  • 1
  • 1
TejjD
  • 2,571
  • 1
  • 17
  • 37
  • thanks for the reply. but i am using this file = new File(Environment.getExternalStorageDirectory() + "/download/img" + rand+".jpg"); file_path= String.valueOf(Uri.fromFile(file)); and this file path i am storing in the Sqlite table – Shubhank Gupta Jan 25 '16 at 06:03
  • Can you please add the rest of your code? Where you are specifying "img_db" – TejjD Jan 25 '16 at 06:05
  • yes you are coreect but when i try this i get the exception E/img_db: file:///storage/sdcard0/download/img15512FILE.jpg-------------> 01-25 14:30:38.522 2509-2509/com.myapp.cowite E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /file:/storage/sdcard0/download/img15512FILE.jpg: open failed: ENOENT (No such file or directory) – Shubhank Gupta Jan 25 '16 at 06:33
  • look at the log, you are still missing a forward slash at : "download/img/15512FILE.jpg" – TejjD Jan 25 '16 at 06:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101560/discussion-between-tejjd-and-shubhank-gupta). – TejjD Jan 25 '16 at 06:37
  • problem is not solve actually i am adding the img as prefix for the image – Shubhank Gupta Jan 25 '16 at 06:55