为什么只有10个图像的1加载和保存在SD卡 [英] why only 1 of 10 image is load and saved in sd card
本文介绍了为什么只有10个图像的1加载和保存在SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我加载从网址存储在阵列10的图像。只有最后一个索引网址加载和SD卡保存图像。这里是我的code:
为(INT J = 0; J<则为list.size(); J ++)
{
reviewImageLink = List.get(J)获得(TAG_Image)的ToString();
URL reviewImageURL;
字符串名称= reviewImageLink; //子(reviewImageLink .lastIndexOf(/)+1,reviewImageLink.length());
尝试{
reviewImageURL =新的URL(reviewImageLink);
如果(!hasExternalStoragePublicPicture(名称)){
isImage = FALSE;
新DownloadImageTask()执行(reviewImageURL);
Log.v(log_tag,如果);
isImage = TRUE;
文件sdImageMainDirectory =新的文件(Environment.getExternalStorageDirectory(),getResources()的getString(R.string.directory));
//if(!sdImageMainDirectory.exists()){
sdImageMainDirectory.mkdirs();
档案文件=新的文件(sdImageMainDirectory,名);
Log.v(log_tag,创建目录);}
}
赶上(MalformedURLException的E){
Log.v(TAG,e.toString()); }
} }//尝试
赶上(例外五){
e.printStackTrace();}DownloaderTask:类DownloadImageTask扩展的AsyncTask< URL,整型,位图> {
//这个类定义指出DownloadImageTask将字符串
//参数,发布整数进度更新,并返回一个位图
保护位图doInBackground(网址...路径){
URL网址;
尝试{
URL =路径[0];
HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();
INT长度= connection.getContentLength();
InputStream为=(InputStream的)url.getContent();
字节[]为imageData =新的字节[长度]
INT缓冲区大小=(int)的Math.ceil(长/(双)100);
INT下载= 0;
INT读;
而(下载<长度){
如果(长度LT;缓冲区大小){
读= is.read(为imageData,下载,长度);
}否则如果((长 - 下载)< =缓冲区大小){
读= is.read(为imageData,下载,下载与长度);
}其他{
读= is.read(为imageData,下载缓冲区大小);
}
下载+ =读;
publishProgress((下载* 100)/长度);
}
位图位图= BitmapFactory.de codeByteArray的(为imageData,0,长度);
如果(位图!= NULL){
Log.i(TAG,位图创建);
}其他{
Log.i(TAG,位图没有创建);
}
is.close();
返回位图;
}赶上(MalformedURLException的E){
Log.e(TAG,畸形异常:+ e.toString());
}赶上(IOException异常五){
Log.e(TAGIOException异常:+ e.toString());
}赶上(例外五){
Log.e(TAG,异常:+ e.toString());
}
返回null; } 保护无效onPostExecute(位图结果){
字符串名称= reviewImageLink.substring(reviewImageLink.lastIndexOf(/)+1,reviewImageLink.length());
如果(结果!= NULL){
hasExternalStoragePublicPicture(名);
saveToSDCard(因此,名);
isImage = TRUE; }其他{
isImage = FALSE; }
}
}公共无效saveToSDCard(位图位图,字符串名称){
布尔mExternalStorageAvailable = FALSE;
布尔mExternalStorageWriteable = FALSE;
字符串状态= Environment.getExternalStorageState();
如果(Environment.MEDIA_MOUNTED.equals(州)){
mExternalStorageAvailable = mExternalStorageWriteable = TRUE;
Log.v(TAG,SD卡仅可用于读取和写入+ mExternalStorageAvailable + mExternalStorageWriteable);
saveFile的(位图名);
}否则如果(Environment.MEDIA_MOUNTED_READ_ONLY.equals(州)){
mExternalStorageAvailable = TRUE;
mExternalStorageWriteable = FALSE;
Log.v(TAG,SD卡仅可用于读取+ mExternalStorageAvailable);
}其他{
mExternalStorageAvailable = mExternalStorageWriteable = FALSE;
Log.v(TAG,请插入SD卡保存录像+ mExternalStorageAvailable + mExternalStorageWriteable);
}
}私人无效了saveFile(位图位图,字符串名称)
{
字符串文件名=名称;
ContentValues值=新ContentValues();
文件sdImageMainDirectory =新的文件(Environment.getExternalStorageDirectory(),getResources()的getString(R.string.directory));
sdImageMainDirectory.mkdirs();
文件OUTPUTFILE =新的文件(sdImageMainDirectory,文件名);
values.put(MediaStore.MediaColumns.DATA,outputFile.toString());
values.put(MediaStore.MediaColumns.TITLE,文件名);
values.put(MediaStore.MediaColumns.DATE_ADDED,System.currentTimeMillis的());
values.put(MediaStore.MediaColumns.MIME_TYPEoutput.jpeg);
URI URI = this.getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
//乌里结果= context.getContentResolver()插入(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,图像)。
sendBroadcast(新意图(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(文件://+ Environment.getExternalStorageDirectory()))); 尝试
{
的OutputStream outStream = this.getContentResolver()openOutputStream(URI)。
bitmap.com preSS(Bitmap.Com pressFormat.JPEG,95,outStream);
outStream.flush();
outStream.close();
}
赶上(FileNotFoundException异常E)
{
e.printStackTrace();
}赶上(IOException异常E)
{
e.printStackTrace();
}
}私人布尔hasExternalStoragePublicPicture(字符串名称){
文件sdImageMainDirectory =新的文件(Environment.getExternalStorageDirectory(),getResources()的getString(R.string.directory));
档案文件=新的文件(sdImageMainDirectory,名);
如果(文件!= NULL)
{
file.delete();
}
返回file.exists();
}
解决方案
通过列表 DownloadImageTask
而不是网址
然后把一个for循环 doInBackground
,
I am loading 10 Images from URL's that are stored in array . only last index url is loading and saving image in SD card . here is my code :
for(int j=0; j<List.size();j++)
{
reviewImageLink =List.get(j).get(TAG_Image).toString();
URL reviewImageURL;
String name = reviewImageLink;//.substring(reviewImageLink .lastIndexOf("/") + 1,reviewImageLink.length());
try {
reviewImageURL = new URL(reviewImageLink);
if (!hasExternalStoragePublicPicture(name)) {
isImage = false;
new DownloadImageTask().execute(reviewImageURL);
Log.v("log_tag", "if");
isImage = true;
File sdImageMainDirectory = new File(Environment.getExternalStorageDirectory(), getResources().getString(R.string.directory));
//if(!sdImageMainDirectory.exists()){
sdImageMainDirectory.mkdirs();
File file = new File(sdImageMainDirectory, name);
Log.v("log_tag", "Directory created");}
}
catch (MalformedURLException e) {
Log.v(TAG, e.toString()); }
}
}//try
catch (Exception e) {
e.printStackTrace();}
DownloaderTask:
class DownloadImageTask extends AsyncTask<URL, Integer, Bitmap> {
// This class definition states that DownloadImageTask will take String
// parameters, publish Integer progress updates, and return a Bitmap
protected Bitmap doInBackground(URL... paths) {
URL url;
try {
url = paths[0];
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int length = connection.getContentLength();
InputStream is = (InputStream) url.getContent();
byte[] imageData = new byte[length];
int buffersize = (int) Math.ceil(length / (double) 100);
int downloaded = 0;
int read;
while (downloaded < length) {
if (length < buffersize) {
read = is.read(imageData, downloaded, length);
} else if ((length - downloaded) <= buffersize) {
read = is.read(imageData, downloaded, length- downloaded);
} else {
read = is.read(imageData, downloaded, buffersize);
}
downloaded += read;
publishProgress((downloaded * 100) / length);
}
Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0,length);
if (bitmap != null) {
Log.i(TAG, "Bitmap created");
} else {
Log.i(TAG, "Bitmap not created");
}
is.close();
return bitmap;
} catch (MalformedURLException e) {
Log.e(TAG, "Malformed exception: " + e.toString());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.toString());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.toString());
}
return null;
}
protected void onPostExecute(Bitmap result) {
String name = reviewImageLink.substring(reviewImageLink.lastIndexOf("/") + 1,reviewImageLink.length());
if (result != null) {
hasExternalStoragePublicPicture(name);
saveToSDCard(result, name);
isImage = true;
} else {
isImage = false;
}
}
}
public void saveToSDCard(Bitmap bitmap, String name) {
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = mExternalStorageWriteable = true;
Log.v(TAG, "SD Card is available for read and write "+ mExternalStorageAvailable + mExternalStorageWriteable);
saveFile(bitmap, name);
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
Log.v(TAG, "SD Card is available for read "+ mExternalStorageAvailable);
} else {
mExternalStorageAvailable = mExternalStorageWriteable = false;
Log.v(TAG, "Please insert a SD Card to save your Video "+ mExternalStorageAvailable + mExternalStorageWriteable);
}
}
private void saveFile(Bitmap bitmap, String name)
{
String filename = name;
ContentValues values = new ContentValues();
File sdImageMainDirectory = new File(Environment.getExternalStorageDirectory(), getResources().getString(R.string.directory));
sdImageMainDirectory.mkdirs();
File outputFile = new File(sdImageMainDirectory, filename);
values.put(MediaStore.MediaColumns.DATA, outputFile.toString());
values.put(MediaStore.MediaColumns.TITLE, filename);
values.put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis());
values.put(MediaStore.MediaColumns.MIME_TYPE, "output.jpeg");
Uri uri = this.getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
//Uri result = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
try
{
OutputStream outStream = this.getContentResolver().openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.JPEG, 95, outStream);
outStream.flush();
outStream.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
private boolean hasExternalStoragePublicPicture(String name) {
File sdImageMainDirectory = new File(Environment.getExternalStorageDirectory(), getResources().getString(R.string.directory));
File file = new File(sdImageMainDirectory, name);
if (file != null)
{
file.delete();
}
return file.exists();
}
解决方案
Pass the list to DownloadImageTask
instead of URL
then put a for loop in doInBackground
,
这篇关于为什么只有10个图像的1加载和保存在SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文