-4

I get data from my service :

        JSONObject userGuid = new JSONObject();
        userGuid.put("userGuid", id);
        Bitmap bitmap = null;
        String response = HttpUtil.post(mService + "GetUser", userGuid.toString(), mCookie);

        JSONObject result = new JSONObject(response).getJSONObject("User");
        String temp = result.getJSONArray("UserImage").toString();

in received data, there is an Base64 image user and I got it and convert it to arryByte and then I convert it to InputStream :

        byte[] tmp = Base64.decode(temp, Base64.DEFAULT);
        InputStream is = new ByteArrayInputStream(tmp);

I want to write it to a File by outputStream :

OutputStream os = new FileOutputStream(f);

but I got this error:

java.io.FileNotFoundException: /storage/emulated/0/fcImages/581864034: open failed: EACCES (Permission denied)

In manifest I added these permissions:

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

What is your Idea?

Edit I create file in this way:

public class FileCache {

private File cacheDir;

public FileCache(Context context) {

    if((Environment
            .getExternalStorageState()).equals(Environment.MEDIA_MOUNTED)) {
            cacheDir = new File(Environment.getExternalStorageDirectory(), "fcImages");
    } else {
        cacheDir = context.getCacheDir();
    }
    if(!cacheDir.mkdirs()) {
        cacheDir.mkdirs();
    }
}

public File getFile(String id) {
    String filename = String.valueOf(id.hashCode());
    File f = new File(cacheDir, filename);
    return f;
}
sayres
  • 361
  • 1
  • 4
  • 15
  • 1
    witch Android OS Version you test ?? – Sanjay Chauhan Aug 28 '17 at 10:41
  • I am using api 23 – sayres Aug 28 '17 at 10:43
  • 3
    Probably you need to read this. You must ask for permission in your code before trying to "use" it :) . https://developer.android.com/training/permissions/requesting.html – Andrei Terecoasa Aug 28 '17 at 10:43
  • the same code is correct in this api but only difference is the other project I used a uri to get images and then write it:`URL imageUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setInstanceFollowRedirects(true); InputStream is = conn.getInputStream(); OutputStream os = new FileOutputStream(f);` – sayres Aug 28 '17 at 10:44
  • 1
    Just check permissions of your application in your device settings. Android api 23 and newest are ignoring your manifest permissions. Now it requires user-interaction to allow it. Check this [link](https://developer.android.com/training/permissions/requesting.html) – grabarz121 Aug 28 '17 at 11:11
  • @grabarz121 I create this file in the recyclerView Adapter. is correct? – sayres Aug 28 '17 at 11:22

2 Answers2

1

There are 3 things that come in my mind to check:

  1. uses-permission tag is inside manifest tag (and not application tag)
  2. EACCESS may due to the fact that fcImages directory does not exist, or it is a file
  3. permission for Android api 23 must be requested runtime

Source for (1) is this answer, while source for (3) is this answer

Matteo Ragni
  • 2,837
  • 1
  • 20
  • 34
0

If you're writing the file to the application's internal storage. Try this:

Example 1:

java.io.File xmlFile = new java.io.File((getActivity()
      .getApplicationContext().getFileStreamPath("FileName.xml")
      .getPath()));

Also give the manifest permission correct way as below:

Example 2 :

<manifest>
        <uses-permission 
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        ...
        <application>
            ...
            <activity> 
                ...
            </activity>
        </application>
 </manifest> 
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53