[英]Save multiple images from drawable folder with one button click
I want to download multiple images from my app drawable
with one button click and save it in device internal/external
storage of my Android app.我想通过一键单击从我的drawable
应用程序中下载多个图像,并将其保存在我的 Android 应用程序的设备internal/external
存储中。
How do I achieve this?我如何实现这一目标?
UPDATE更新
I tried it, but I'm having one issue.我试过了,但我有一个问题。
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++;
}
};
});
The issue is that it's not the images are not downloading.问题是不是图像没有下载。
**This is what my "Run" log is showing ** **这是我的“运行”日志显示的内容 **
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.
I assume that by "download" you mean save/export.我认为“下载”是指保存/导出。 Then, this code should be working fine.然后,此代码应该可以正常工作。
Option 1:选项1:
If you want to save images to external storage (SD card) add this line above the code:如果要将图像保存到外部存储(SD 卡),请在代码上方添加以下行:
String path = new File(Environment.getExternalStorageDirectory(), "Images").getAbsolutePath();
Add : <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to manifest添加: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
到清单
Option 2选项 2
If you want to save images to internal storage, just add:如果要将图像保存到内部存储器,只需添加:
String path = new File(getFilesDir(), "Images").getAbsolutePath() ;
Then:然后:
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++;
}
});
Output:输出:
R.drawable.imageId1
will be saved to path/image1.PNG
R.drawable.imageId1
将被保存到path/image1.PNG
R.drawable.imageId2
will be saved to path/image2.PNG
R.drawable.imageId2
将被保存到path/image2.PNG
R.drawable.imageId3
will be saved to path/image3.PNG
R.drawable.imageId3
将被保存到path/image3.PNG
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.