Android - 将下载的图像从 URL 保存到 SD 卡上
我在单击按钮时从 URL 加载图像,并将其存储为位图。现在我想知道如何将下载的图像保存到 SD 卡以及系统中。
我尝试按以下方式执行此操作:
package com.v3.thread.fetchImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainThreadActivity extends Activity {
ImageView imView;
EditText ed1;
Bitmap bmImg;
Button bt, btSave;
String imageUrl = "";
int visibilty = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.edURL);
btSave = (Button) findViewById(R.id.btnSave);
bt = (Button) findViewById(R.id.btnLoad);
bt.setOnClickListener(getImgListener);
imView = (ImageView) findViewById(R.id.imview);
Log.i("img already downloaded", "img");
btSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Log.i("img url", "Under Save");
saveImage();
}
});
}
View.OnClickListener getImgListener = new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
imageUrl = ed1.getText().toString();
if (imageUrl.equals(""))
Toast.makeText(getApplicationContext(), "Enter an URL first", 1000).show();
downloadFile(imageUrl);
Log.i("im url", imageUrl);
btSave.setVisibility(visibilty);
}
};
void downloadFile(String fileUrl) {
URL myFileUrl = null;
try {
myFileUrl = new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Log.i("im connected", "Download");
bmImg = BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void saveImage() {
File filename;
try {
String path = Environment.getExternalStorageDirectory().toString();
Log.i("in save()", "after mkdir");
new File(path + "/mvc/mvc").mkdir();
filename = new File(path + "/mvc/mvc/var3.jpg");
Log.i("in save()", "after file");
FileOutputStream out = new FileOutputStream(filename);
Log.i("in save()", "after outputstream");
bmImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Log.i("in save()", "after outputstream closed");
MediaStore.Images.Media.insertImage(getContentResolver(),
filename.getAbsolutePath(), filename.getName(),
filename.getName());
bt.setText("Saved...");
Toast.makeText(getApplicationContext(),
"File is Saved in " + filename, 1000).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
从 URL 加载图像正在工作,但是当我按“保存”按钮保存它时,它会抛出异常
java.io.FileNotFoundException:/mnt/sdcard/mvc/mvc/var3.image(没有这样的 文件或目录)
那么如何正确将图像保存到SD卡中呢?
I am loading an image from an URL on button click, and storing it as a Bitmap. Now i want to know how to save that downloaded image into sd card as well as in system.
I attempted to do it the following way:
package com.v3.thread.fetchImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainThreadActivity extends Activity {
ImageView imView;
EditText ed1;
Bitmap bmImg;
Button bt, btSave;
String imageUrl = "";
int visibilty = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.edURL);
btSave = (Button) findViewById(R.id.btnSave);
bt = (Button) findViewById(R.id.btnLoad);
bt.setOnClickListener(getImgListener);
imView = (ImageView) findViewById(R.id.imview);
Log.i("img already downloaded", "img");
btSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Log.i("img url", "Under Save");
saveImage();
}
});
}
View.OnClickListener getImgListener = new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
imageUrl = ed1.getText().toString();
if (imageUrl.equals(""))
Toast.makeText(getApplicationContext(), "Enter an URL first", 1000).show();
downloadFile(imageUrl);
Log.i("im url", imageUrl);
btSave.setVisibility(visibilty);
}
};
void downloadFile(String fileUrl) {
URL myFileUrl = null;
try {
myFileUrl = new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Log.i("im connected", "Download");
bmImg = BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void saveImage() {
File filename;
try {
String path = Environment.getExternalStorageDirectory().toString();
Log.i("in save()", "after mkdir");
new File(path + "/mvc/mvc").mkdir();
filename = new File(path + "/mvc/mvc/var3.jpg");
Log.i("in save()", "after file");
FileOutputStream out = new FileOutputStream(filename);
Log.i("in save()", "after outputstream");
bmImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Log.i("in save()", "after outputstream closed");
MediaStore.Images.Media.insertImage(getContentResolver(),
filename.getAbsolutePath(), filename.getName(),
filename.getName());
bt.setText("Saved...");
Toast.makeText(getApplicationContext(),
"File is Saved in " + filename, 1000).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Loading of image from URL is working, but when I press the save button to save it, it throws the exception
java.io.FileNotFoundException: /mnt/sdcard/mvc/mvc/var3.image(No such
file or directory)
So how do I save the image to the SD card correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要首先创建要在其中创建文件的目录和子目录。
我看到您使用了 mkdir() 方法。尝试 mkdirs(),它应该可以工作。
You will need to first create the directories and sub-directories where you want to create the files.
I see that you used the mkdir() method. Try mkdirs(), and it should work.
在清单中添加 WRITE_EXTERNAL_STORAGE android 权限:
然后
从 SD 卡获取目录(文件对象),例如:
创建用于图像存储的特定文件:
然后,
Add WRITE_EXTERNAL_STORAGE android permission in your Manifest:
Then
Get to directory (a File object) from SD Card such as:
Create your specific file for image storage:
Then,