简体   繁体   中英

Android: Saving .png from URL into app internal storage

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM