Android:将.png从URL保存到应用内部存储中 [英] Android: Saving .png from URL into app internal storage

查看:125
本文介绍了Android:将.png从URL保存到应用内部存储中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对android来说还比较陌生,我正在尝试修改一个android应用,以使其从URL下载个人资料图片(最好是在PNG中),并将其保存在com.companyName.AppName.whatever/files中.应该注意的是,该应用最初是在Unity中创建的,只是构建和导出的.

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.

这是我的初始代码:

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

这是@Ashutosh Sagar建议的其他代码

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

(它也不写).

不幸的是,我遇到了一些问题.我的主要问题是,当它运行时(在确实存在的情况下),实际上并没有保存任何内容.我将去检查com.companyName.AppName.whatever/files目录,只是没有找到这样的.png文件.我还将需要它来覆盖所有同名的现有文件,这在不起作用时很难检查.

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.

我的第二个问题是它没有考虑互联网连接的延迟.尽管我已经放入了足够的try-catch子句来阻止它崩溃(就像以前那样),但最终结果是它也没有保存.

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?

打印出目录显示它应该位于:

Printing out the directory reveals it should be in:

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

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

但是,使用ES File Explorer,唯一靠近它的是

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

模拟/0/Android/data/com.appName/文件/

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

它们是同一目录吗?

推荐答案

尝试一下

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

这篇关于Android:将.png从URL保存到应用内部存储中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆