save image in android

188 views Asked by At

This is a code for saving images in SD card if and if not exist. but i don't know how to read it. Can anybody help me please.

This is the download file method:

public static String DownLoadFile(String netUrl,  String name ) {

          try {
                    //need uses permission WRITE_EXTERNAL_STORAGE

                ByteArrayBuffer baf = null;
                long startTime = 0;

                //get to directory (a File object) from SD Card
                    File savePath=new File(Environment.getExternalStorageDirectory().getPath()+"/postImages/");
                    String ext="jpg";
                URL url = new URL(netUrl);


                //create your specific file for image storage:
                File file = new File(savePath, name + "." + ext);
                boolean success = true;
                if (!savePath.exists()) {
                    success = savePath.mkdir();
                }
                if (success) {
                    if(file.createNewFile())
                      {
                        file.createNewFile();


                    //write the Bitmap
                    Log.i("file existence", "file does not exist!!!!!!!!!!!");
           /* Open a connection to that URL. */
                URLConnection ucon = url.openConnection();

                InputStream is = ucon.getInputStream();

                BufferedInputStream bis = new BufferedInputStream(is);
                startTime = System.currentTimeMillis();


                baf = new ByteArrayBuffer(5000);
                int current = 0;
                while ((current = bis.read()) != -1) {
                    baf.append((byte) current);
                }



                   /* Convert the Bytes read to a String. */
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(baf.toByteArray());
                fos.flush();
                fos.close();
                Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");

                return file.getAbsolutePath();
                      }//end of create file if not exists

                }//end of if success


            } catch (Exception exx) {
                if (exx.getMessage() != null) {


                } else {


                }

            }

            return null;
        }
2

There are 2 answers

0
Brinda K On

Try this,

Uri uri = Uri.parse("file:///sdcard/temporary_file.jpg");

img.setImageURI(uri);

0
Sandeep Patidar On
  if u have image uri so get path from uri like
 String Path = fileUri.getPath();   

  // read file from sdcard       

  public static byte[] readFromStream(String path) throws Exception { File
  file = new File(path); InputStream inputStream = new
  FileInputStream(file); ByteArrayOutputStream baos = new
  ByteArrayOutputStream(); DataOutputStream dos = new
  DataOutputStream(baos); byte[] data = new byte[(int) file.length()]; int
  count = inputStream.read(data); while (count != -1) { dos.write(data, 0,
  count); count = inputStream.read(data); } return baos.toByteArray(); }