简体   繁体   English

Android:将.png从URL保存到应用内部存储中

[英]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 . 我是android的新手,我试图修改一个android应用程序,使其从URL下载个人资料图片(最好是PNG),并将其保存在com.companyName.AppName.whatever/files It should be noted that the app was initially created in Unity, and just built and exported. 应该注意的是,该应用最初是在Unity中创建的,只是构建和导出的。

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 编辑:这是我的其他代码,如@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. 我将去检查com.companyName.AppName.whatever/files目录,仅发现没有这样的.png文件。 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. 尽管我已经放入了足够的try-catch子句来阻止它崩溃(就像以前那样),但最终结果是它也没有保存。

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 /data/user/0/com.appName/files/5965e9e4a0f0463853016e2b.png

However, using ES File explorer, the only thing remotely close to that is 但是,使用ES File Explorer,唯一接近它的地方是

emulated/0/Android/data/com.appName/files/ 模拟/ 0 / Android设备/数据/ com.appName /文件/

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 并保存位图图像,请检查 GoCrazy 的ans

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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