我想一键从我的应用drawable
下载多张图片,并将其保存在我的Android应用的设备internal/external
存储中。
我该如何实现?
更新
我尝试过,但是有一个问题。
downloadTest.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View v){
//path here is for internal storage (Option 2), replace it with the other line above from Option 1 to save images to SD card
String path = new File(getFilesDir(), "/Space Wallpapers").getAbsolutePath();
//ids are drawables resource ids
int[] ids = new int[]{ R.drawable.abduction,
R.drawable.green_nightstreak,
R.drawable.hero,
R.drawable.ic_night,
R.drawable.light_streak,
R.drawable.moon_with_stars,
R.drawable.orange_nightstreak,};
int i = 1;
for (int res : ids) {
//Creates the bitmap object from the given resource
Bitmap b = BitmapFactory.decodeResource(getResources(), res);
File file = new File(path, "image" + i + ".PNG");
//This creates the file and it's parent if doesn't exist
file.getParentFile().mkdirs();
try {
FileOutputStream fileOut = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.PNG, 100, fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
i++;
}
};
});
问题在于不是图像未下载。
**这是我的“运行”日志显示的内容**
D/ViewRootImpl@372fd66[TestActivity]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@372fd66[TestActivity]: ViewPostImeInputStage processPointer 1
W/System.err: java.io.FileNotFoundException: /data/user/0/com.khumomashapa.notes.debug/files/Space Wallpapers/image1.PNG (Is a directory)
at java.io.FileOutputStream.open(Native Method)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at com.khumomashapa.notes.activities.TestActivity$1.onClick(TestActivity.java:54)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
W/System.err: at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1073)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
W/System.err: java.io.FileNotFoundException: /data/user/0/com.khumomashapa.notes.debug/files/Space Wallpapers/image2.PNG (Is a directory)
at java.io.FileOutputStream.open(Native Method)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at com.khumomashapa.notes.activities.TestActivity$1.onClick(TestActivity.java:54)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1073)
W/System.err: at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
W/System.err: java.io.FileNotFoundException: /data/user/0/com.khumomashapa.notes.debug/files/Space Wallpapers/image3.PNG (Is a directory)
W/System.err: at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at com.khumomashapa.notes.activities.TestActivity$1.onClick(TestActivity.java:54)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1073)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
W/System.err: java.io.FileNotFoundException: /data/user/0/com.khumomashapa.notes.debug/files/Space Wallpapers/image4.PNG (Is a directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at com.khumomashapa.notes.activities.TestActivity$1.onClick(TestActivity.java:54)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1073)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
W/System.err: java.io.FileNotFoundException: /data/user/0/com.khumomashapa.notes.debug/files/Space Wallpapers/image5.PNG (Is a directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at com.khumomashapa.notes.activities.TestActivity$1.onClick(TestActivity.java:54)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1073)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
W/System.err: java.io.FileNotFoundException: /data/user/0/com.khumomashapa.notes.debug/files/Space Wallpapers/image6.PNG (Is a directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at com.khumomashapa.notes.activities.TestActivity$1.onClick(TestActivity.java:54)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1073)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
W/System.err: java.io.FileNotFoundException: /data/user/0/com.khumomashapa.notes.debug/files/Space Wallpapers/image7.PNG (Is a directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
W/System.err: at com.khumomashapa.notes.activities.TestActivity$1.onClick(TestActivity.java:54)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1073)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:154)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6682)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
I/Choreographer: Skipped 134 frames! The application may be doing too much work on its main thread.
答案 0 :(得分:0)
我认为“下载”是指保存/导出。然后,此代码应该可以正常工作。
选项1:
如果要将图像保存到外部存储(SD卡),请在代码上方添加以下行:
String path = new File(Environment.getExternalStorageDirectory(), "Images").getAbsolutePath();
添加:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
以显示
选项2
如果要将图像保存到内部存储,只需添加:
String path = new File(getFilesDir(), "Images").getAbsolutePath() ;
然后:
yourButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View v){
//path here is for internal storage (Option 2), replace it with the other line above from Option 1 to save images to SD card
String path = new File(getFilesDir(), "Images").getAbsolutePath();
//ids are drawables resource ids
int[] ids = new int[]{R.drawable.imageId1, R.drawable.imageId2, R.drawable.imageId3};
int i = 1;
for (int res : ids) {
//Creates the bitmap object from the given resource
Bitmap b = BitmapFactory.decodeResource(getResources(), res);
File file = new File(path, "image" + i + ".PNG");
//This creates the file and it's parent if doesn't exist
file.getParentFile().mkdirs();
try {
FileOutputStream fileOut = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.PNG, 100, fileOut);
fileOut.close;
} catch (Exception e) {
e.printStackTrace();
}
i++;
}
});
输出:
R.drawable.imageId1
将保存到path/image1.PNG
R.drawable.imageId2
将保存到path/image2.PNG
R.drawable.imageId3
将保存到path/image3.PNG