Android开发将网络图片保存到相册

释放双眼,带上耳机,听听看~!

这是我所见过的堪称最最最最简单的将图片保存到相册中的方法Plus

废话不多说,直接撸代码

1.对于权限的注册

   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>

安卓6.0以后一定要加读写权限的判断 否则是保存不了的 这是重点
2.工具类

public class DonwloadSaveImg {
private static Context context;
private static String filePath;
private static Bitmap mBitmap;
private static String mSaveMessage = "失败";
private final static String TAG = "PictureActivity";
private static ProgressDialog mSaveDialog = null;
public static void donwloadImg(Context contexts, String filePaths) {
context = contexts;
filePath = filePaths;
mSaveDialog = ProgressDialog.show(context, "保存图片", "图片正在保存中,请稍等...", true);
new Thread(saveFileRunnable).start();
}
private static Runnable saveFileRunnable = new Runnable() {
@Override
public void run() {
try {
if (!TextUtils.isEmpty(filePath)) { //网络图片
// 对资源链接
URL url = new URL(filePath);
//打开输入流
InputStream inputStream = url.openStream();
//对网上资源进行下载转换位图图片
mBitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
}
saveFile(mBitmap);
mSaveMessage = "图片保存成功!";
} catch (IOException e) {
mSaveMessage = "图片保存失败!";
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
messageHandler.sendMessage(messageHandler.obtainMessage());
}
};
private static Handler messageHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
mSaveDialog.dismiss();
Log.d(TAG, mSaveMessage);
Toast.makeText(context, mSaveMessage, Toast.LENGTH_SHORT).show();
}
};
/**
* 保存图片
* @param bm
* @throws IOException
*/
public static void saveFile(Bitmap bm ) throws IOException {
File dirFile = new File(Environment.getExternalStorageDirectory().getPath());
if (!dirFile.exists()) {
dirFile.mkdir();
}
String fileName = UUID.randomUUID().toString() + ".jpg";
File myCaptureFile = new File(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/" + fileName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
//把图片保存后声明这个广播事件通知系统相册有新图片到来
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(myCaptureFile);
intent.setData(uri);
context.sendBroadcast(intent);
}

3.主活动中权限的判断以及保存图片

public class Main2Activity extends AppCompatActivity {
private static int REQUEST_PERMISSION_CODE = 1;
private String path="https://www.baidu.com/img/bd_logo1.png?where=super";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
checkPermission();
}
private void checkPermission() {
//检查权限(NEED_PERMISSION)是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//用户已经拒绝过一次,再次弹出权限申请对话框需要给用户一个解释
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission
.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(this, "请开通相关权限,否则无法正常使用本应用!", Toast.LENGTH_SHORT).show();
}
//申请权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_CODE);
} else {
Toast.makeText(this, "授权成功!", Toast.LENGTH_SHORT).show();
Log.e("aaaaa", "checkPermission: 已经授权!");
}
}
public void write(View view) {   //按钮的点击事件
DonwloadSaveImg.donwloadImg(Main2Activity.this,path);//iPath
}
}

大功告成!!!!!!希望能够对你有用
反正我是亲测有效

人已赞赏
Android文章

Android开发启动其他应用程序的组件

2020-3-11 15:11:35

Android文章

Android sqlite数据库update之后数据查询数据获取不实时。。。

2020-3-11 15:48:51

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索