0

I'm relatively new to android, and I'm trying to modify an android app such that it downloads a profile picture (preferably in PNG) from a URL, and saves it in the com.companyName.AppName.whatever/files. It should be noted that the app was initially created in Unity, and just built and exported.

Here's my initial code:

URL url = null;
try {
    url = new URL(playerDO.getProfileURL());
} catch (MalformedURLException e) {
    e.printStackTrace();
}

InputStream input = null;
try {
    input = url.openStream();
} catch (IOException e) {
    e.printStackTrace();
}

String fileName = playerDO.getId() + ".png";
FileOutputStream outputStream = null;
try {
    outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);

    byte[] buffer = new byte[256];
    int bytesRead = 0;
    while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
        outputStream.write(buffer, 0, bytesRead);
    }    
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        outputStream.close();
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

EDIT: Here's my other code, as suggested by @Ashutosh Sagar

InputStream input = null;
Bitmap image = null;
try {
    input = url.openStream();
    image = BitmapFactory.decodeStream(input);
} catch (IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

String fileName = playerDO.getId() + ".png";
FileOutputStream outputStream = null;
File myDir = getFilesDir();

try {
    Log.wtf("DIRECTORY", myDir.toString());
    File imageFile = new File(myDir, fileName);
    if (!imageFile.exists()){
        imageFile.createNewFile();
        Log.wtf("ANDROID NATIVE MSG: WARN!", "File does not exist. Writing to: " + imageFile.toString());
    }

    outputStream = new FileOutputStream(imageFile, false);
    image.compress(Bitmap.CompressFormat.PNG, 90, outputStream);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        outputStream.close();
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
        Log.wtf("AWWW CRAP", e.toString());
    }
}

(It doesn't write either).

Unfortunately, I've had several problems with this. My primary issue is that when it (on the cases that it does) runs, it actually doesn't save anything. I'll go and check com.companyName.AppName.whatever/files directory only to find no such .png file. I will also need it to overwrite any existing files of the same name, which is hard to check when it doesn't work.

My secondary issue is that it fails to take into account delays in internet connection. Although I've put in enough try-catch clauses to stop it from crashing (as it used to), the end result is that it also doesn't save.

How can I improve upon this? Anything I'm missing?

EDIT:

Printing out the directory reveals it should be in:

/data/user/0/com.appName/files/5965e9e4a0f0463853016e2b.png

However, using ES File explorer, the only thing remotely close to that is

emulated/0/Android/data/com.appName/files/

Are they the same directory?

zack_falcon
  • 4,186
  • 20
  • 62
  • 108

2 Answers2

0

try this first get bitmap image from url

 Bitmap mIcon11 = null;
try {
    InputStream in = new java.net.URL(url).openStream();
    mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
    Log.d("Error", e.getStackTrace().toString());

}

and to save bitmap image please check the ans of GoCrazy

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • The link you pointed to saves in the external storage. How do I go about with using internal storage as opposed to external? – zack_falcon Jul 24 '17 at 08:06
0

Try this

void getImage(String string_url)
{
    //Generate Bitmap from URL
    URL url_value = new URL(string_url);
    Bitmap image =
    BitmapFactory.decodeStream(url_value.openConnection().getInputStream());

    //Export File to local Directory
    OutputStream stream = new FileOutputStream("path/file_name.png");
    /* Write bitmap to file using JPEG or PNG and 80% quality hint for JPEG. */
    bitmap.compress(CompressFormat.PNG, 80, stream);
    stream.close();
}
KamikyIT
  • 314
  • 2
  • 15
Ashutosh Sagar
  • 981
  • 8
  • 20
  • It doesn't work, for some reason. But I've updated the question, perhaps that could shed some light on this. – zack_falcon Jul 24 '17 at 11:22
  • https://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog try this if could be some help – Ashutosh Sagar Jul 24 '17 at 11:45