Java 从Android中的本地隐藏文件夹下载图像并显示
我正在尝试从internet下载图像,并将其存储在某个隐藏文件夹中,用户在其图库中看不到这些图像 这是我的代码。只有当设备连接到internet时,才会显示图像。换句话说,图像没有保存在设备中,而是引发异常 这是我的ImageStorage类:Java 从Android中的本地隐藏文件夹下载图像并显示,java,android,file-handling,android-bitmap,android-download-manager,Java,Android,File Handling,Android Bitmap,Android Download Manager,我正在尝试从internet下载图像,并将其存储在某个隐藏文件夹中,用户在其图库中看不到这些图像 这是我的代码。只有当设备连接到internet时,才会显示图像。换句话说,图像没有保存在设备中,而是引发异常 这是我的ImageStorage类: package com.example.adhish.downloadretriveimage; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import
package com.example.adhish.downloadretriveimage;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by appy-20 on 6/1/17.
*/
public class ImageStorage {
public static String saveToSdCard(Bitmap bitmap, String filename) {
String stored = null;
File sdcard = Environment.getExternalStorageDirectory() ;
File folder = new File(sdcard.getAbsoluteFile(), ".test_directory");//the dot makes this directory hidden to the user
folder.mkdir();
File file = new File(folder.getAbsoluteFile(), filename + ".jpg") ;
if (file.exists())
return stored ;
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
stored = "success";
} catch (Exception e) {
e.printStackTrace();
}
return stored;
}
public static File getImage(String imagename) {
File mediaImage = null;
try {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root);
if (!myDir.exists())
return null;
mediaImage = new File(myDir.getPath() + "/.test_directory/"+imagename);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mediaImage;
}
public static boolean checkifImageExists(String imagename)
{
Bitmap b = null ;
File file = ImageStorage.getImage("/"+imagename+".jpg");
String path = file.getAbsolutePath();
if (path != null)
b = BitmapFactory.decodeFile(path);
if(b == null || b.equals(""))
{
return false ;
}
return true ;
}
}
下面是我的MainActivity.java:
package com.example.adhish.downloadretriveimage;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
Bitmap b;
String imagename;
String imgurl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)findViewById(R.id.imageView);
imagename="394968_538b_7";
imgurl="https://udemy-images.udemy.com/course/750x422/394968_538b_7.jpg";
if(ImageStorage.checkifImageExists(imagename))
{
File file = ImageStorage.getImage("/"+imagename+".jpg");
// File file = ImageStorage.getImage("https://udemy-images.udemy.com/course/750x422/394968_538b_7.jpg");
String path = file.getAbsolutePath();
if (path != null){
b = BitmapFactory.decodeFile(path);
imageView.setImageBitmap(b);
}
} else {
new GetImages(imgurl, imageView, imagename).execute() ;
}
}
private class GetImages extends AsyncTask<Object, Object, Object> {
private String requestUrl, imagename_;
private ImageView view;
private Bitmap bitmap;
private FileOutputStream fos;
private GetImages(String requestUrl, ImageView view, String _imagename_) {
this.requestUrl = requestUrl;
this.view = view;
this.imagename_ = _imagename_;
}
@Override
protected Object doInBackground(Object... objects) {
try {
URL url = new URL(requestUrl);
URLConnection conn = url.openConnection();
bitmap = BitmapFactory.decodeStream(conn.getInputStream());
} catch (Exception ex) {
}
return null;
}
@Override
protected void onPostExecute(Object o) {
if (!ImageStorage.checkifImageExists(imagename_)) {
view.setImageBitmap(bitmap);
ImageStorage.saveToSdCard(bitmap, imagename_);
}
}
}
}
package com.example.adhish.downloadRetrieveImage;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.os.AsyncTask;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.widget.ImageView;
导入java.io.File;
导入java.io.FileOutputStream;
导入java.net.URL;
导入java.net.URLConnection;
公共类MainActivity扩展了AppCompatActivity{
图像视图图像视图;
位图b;
字符串图像名称;
字符串imgurl;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(imageView)findViewById(R.id.imageView);
imagename=“394968_538b_7”;
imgurl=”https://udemy-images.udemy.com/course/750x422/394968_538b_7.jpg";
if(ImageStorage.checkifImageExists(imagename))
{
File File=ImageStorage.getImage(“/”+imagename+“.jpg”);
//File File=ImageStorage.getImage(“https://udemy-images.udemy.com/course/750x422/394968_538b_7.jpg");
字符串路径=file.getAbsolutePath();
if(路径!=null){
b=位图工厂.decodeFile(路径);
设置图像位图(b);
}
}否则{
新建GetImages(imgurl、imageView、imagename).execute();
}
}
私有类GetImages扩展异步任务{
私有字符串requestUrl,imagename;
私有图像视图;
私有位图;
私有文件输出流;
私有GetImages(字符串requestUrl、ImageView视图、字符串\u imagename\u){
this.requestUrl=requestUrl;
this.view=视图;
this.imagename=\u imagename;
}
@凌驾
受保护对象doInBackground(对象…对象){
试一试{
URL=新URL(请求URL);
URLConnection conn=url.openConnection();
位图=BitmapFactory.decodeStream(conn.getInputStream());
}捕获(例外情况除外){
}
返回null;
}
@凌驾
受保护的void onPostExecute(对象o){
如果(!ImageStorage.checkifImageExists(imagename_41;)){
view.setImageBitmap(位图);
saveToSdCard(位图、图像名称);
}
}
}
}
我已在清单中授予外部存储器读写权限。我找到了此问题的解决方案。我将图像存储在SQLite数据库中,下面是它的代码:
我找到了这个问题的解决办法。我将图像存储在SQLite数据库中,下面是它的代码:
显示logcat输出。@KNeerajLal 01-07 12:19:06.535 26907-26907/com.example.adhish.downloadRetrieveImage W/System.err:java.io.FileNotFoundException:394968_538b_7:打开失败:EROFS(只读文件系统)是否在运行时请求android 6的权限?您可以添加日志并检查文件的路径吗?我注意到path.at.com.example.adhish.downloadRetreviemage.ImageStorage.saveToSdCard(ImageStorage.java:46)@user5599807中的eötra“/”,没有在运行时请求权限。另外,额外的“/”在哪里?提前感谢。显示logcat输出。@KNeerajLal 01-07 12:19:06.535 26907-26907/com.example.adhish.downloadRetrieveImage W/System.err:java.io.FileNotFoundException:394968_538b_7:打开失败:EROFS(只读文件系统)您是否在运行时请求android 6的权限?您可以添加日志并检查文件的路径吗?我注意到path.at.com.example.adhish.downloadRetreviemage.ImageStorage.saveToSdCard(ImageStorage.java:46)@user5599807中的eötra“/”,没有在运行时请求权限。另外,额外的“/”在哪里?提前谢谢。