1

Helo, friends i trying to do one image download from the internet using bound services,.through download method i call to bound service class,and through this service one asyctask class is called to display doenload progress through doInBackground(URL... urls) methods. but when output = new FileOutputStream(Path+"hive.jpg"); is called at that time error java.io.FileNotFoundException: /mnt/sdcard/hive.jpg: open failed: EACCES (Permission denied) occure.please help quickly, thanks in advance.

I taking all the permission that required. my menifist file permission :

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.READ_CONTACTS" />

my backgrounds methods code is following:

    @Override
protected Long doInBackground(URL... urls) {

    int count;

        try {
            System.out.println("do in backgrounds");
            URL url = new URL(urls[0].toString());
            System.out.println("utl=" + url);

            URLConnection conexion = url.openConnection();
            System.out.println("open connecton+" + conexion);
            conexion.connect();

            int lenghtOfFile = conexion.getContentLength();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
            InputStream input = new BufferedInputStream(url.openStream());
            System.out.println("input stream=" + input);

            String Path = Environment.getExternalStorageDirectory()
                    + File.separator;// "/sdcard/";

            System.out.println("PAth=" + Path);

            try {
                output = new FileOutputStream(Path+"hive.jpg");  // exception error here
                System.out.println("output stream=" + output);
            } catch (Exception e) {
                e.printStackTrace();
            }
            // OutputStream output = new FileOutputStream(Path+"hive.jpg");

            byte data[] = new byte[1024];

            long total = 0;

            System.out.println("before while");
            while ((count = input.read(data)) != -1) {
                total += count;
                int percent = (int) ((total * 100) / lenghtOfFile);
                publishProgress(percent);
                output.write(data, 0, count);
                System.out.println("file write successfulyy");
            }

            output.flush();
            output.close();
            input.close();
            System.out.println("close function ");
        } catch (Exception e) {
        }
    //}
    return null;

}

Error/Exception are following:

  : java.io.FileNotFoundException: /mnt/sdcard/hive.jpg: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:416)
at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
    at com.example.boundservice2.BackgroundDownload.doInBackground(BackgroundDownload.java:63)
    at com.example.boundservice2.BackgroundDownload.doInBackground(BackgroundDownload.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
   Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at libcore.io.IoBridge.open(IoBridge.java:400)
Lucifer
  • 29,392
  • 25
  • 90
  • 143

1 Answers1

0

Please change your code to below code,

File file = new File( Path + "hive.jpg" );     // Add this line 
output = new FileOutputStream ( file );

if (!file.exists())                           // Add this code 
{
    file.createNewFile();
}
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • yes its give same error point to `output = new FileOutputStream ( file );` –  Apr 08 '14 at 07:24
  • I suggest you to try out your code in real device once. – Lucifer Apr 08 '14 at 07:51
  • Hello good morning kedar, i try it in real device nothing error but image are originally in internet address but error log giving below, –  Apr 09 '14 at 05:21
  • `04-09 10:49:44.092: I/System.out(14162): utl=http://api.androidhive.info/progressdialog/hive.jpg 04-09 10:49:44.096: I/System.out(14162): open connecton+libcore.net.http.HttpURLConnectionImpl:http://api.androidhive.info/progressdialog/hive.jpg 04-09 10:49:44.103: D/libc-netbsd(14162): getaddrinfo: api.androidhive.info NO result from proxy 04-09 10:49:44.104: I/System.out(14162): [CDS][DNS]Unable to resolve host "api.androidhive.info": No address associated with hostname` –  Apr 09 '14 at 05:21
  • Good Morning, can you give me the link of the tutorial ? – Lucifer Apr 09 '14 at 05:25
  • i generally guide from the andriud github and image link is `"http://api.androidhive.info/progressdialog/hive.jpg"` –  Apr 09 '14 at 05:35
  • I know that url, but I want url of the tutorial from where you got this code ? – Lucifer Apr 09 '14 at 05:36
  • Hello kedar now it is working fine in real device ..thanks for your responce and your quick answers. i just change the link of image. –  Apr 09 '14 at 06:02
  • thanks, just minute kedar,i want to put progress bar when time of downloading how i can do because in `BackgroundDownload` class not supporting post execute and other create dialog, really i want to create one apps that download do in background service so activity can not take much weight. when download over progressbar over and service work over return to activity download load is over so when i put code of progressbar n as i say above –  Apr 09 '14 at 06:09
  • you can do it in AsyncTask, too. See this answer http://stackoverflow.com/a/3893691/3330969 – Lucifer Apr 09 '14 at 06:13
  • hello kedar i so much tried but not got at the solution u give me any example of bound service that download image display in progressbar and at the over progress dismiss using only bound service ??? –  Apr 09 '14 at 10:51
  • did you check that answer ? – Lucifer Apr 09 '14 at 10:59