Android 下载到APP私有目录的图片和视频同步到手机图库相册

//下载到本地私有目录的代码省略
if (file.getAbsolutePath().contains("MP4")) {
						copyFileToGallery(file, file.getName());
					} else {
						MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), BitmapFactory.decodeFile(file.getAbsolutePath()), file.getName(), null);
						Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
						Uri uri11 = Uri.fromFile(file);
						intent.setData(uri11);
						getActivity().sendBroadcast(intent);
					}



private void copyFileToGallery(File file, String fileName) {
		Uri uriSavedVideo;
		ContentResolver resolver = getActivity().getContentResolver();
		ContentValues valuesVideos;
		valuesVideos = new ContentValues();

		if (Build.VERSION.SDK_INT >= 29) {
			valuesVideos.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" + "kacam");
			valuesVideos.put(MediaStore.Video.Media.TITLE, fileName);
			valuesVideos.put(MediaStore.Video.Media.DISPLAY_NAME, fileName);
			valuesVideos.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
			valuesVideos.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
			valuesVideos.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
			valuesVideos.put(MediaStore.Video.Media.IS_PENDING, 1);

			Uri collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
			uriSavedVideo = resolver.insert(collection, valuesVideos);
		} else {

			valuesVideos.put(MediaStore.Video.Media.TITLE, fileName);
			valuesVideos.put(MediaStore.Video.Media.DISPLAY_NAME, fileName);
			valuesVideos.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
			valuesVideos.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
			//valuesVideos.put(MediaStore.Video.Media.DATA, file.getAbsolutePath());
			uriSavedVideo = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, valuesVideos);
		}

		ParcelFileDescriptor pfd;
		try {
			pfd = resolver.openFileDescriptor(uriSavedVideo, "w");
			FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());
			FileInputStream in = new FileInputStream(file);

			byte[] buf = new byte[8192];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}

			out.close();
			in.close();
			pfd.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (Build.VERSION.SDK_INT >= 29) {
			valuesVideos.clear();
			valuesVideos.put(MediaStore.Video.Media.IS_PENDING, 0);
			resolver.update(uriSavedVideo, valuesVideos, null, null);
		}
	}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android应用程序的私有目录是指每个应用程序在安装时会创建一个私有目录,只有该应用程序可以访问该目录中的文件,其他应用程序无法访问。私有目录通常包括应用程序的缓存目录、数据库目录和共享首选项目录等。 读取私有目录中的文件 要访问应用程序的私有目录中的文件,可以使用Context对象提供的openFileInput()方法获取一个FileInputStream对象,然后使用该对象读取文件内容。例如: ```java try { FileInputStream fis = openFileInput("file.txt"); byte[] buffer = new byte[fis.available()]; fis.read(buffer); String content = new String(buffer); fis.close(); } catch (IOException e) { e.printStackTrace(); } ``` 在此示例中,我们打开名为“file.txt”的文件,并使用Java的FileInputStream类读取文件内容。我们使用fis.available()方法获取文件的大小,然后创建一个大小等于文件大小的字节数组来存储文件内容。最后,我们使用Java的String类将字节数组转换为字符串。 写入私有目录中的文件 要将数据写入应用程序的私有目录中的文件,可以使用Context对象提供的openFileOutput()方法获取一个FileOutputStream对象,然后使用该对象将数据写入文件。例如: ```java try { FileOutputStream fos = openFileOutput("file.txt", Context.MODE_PRIVATE); fos.write("Hello World".getBytes()); fos.close(); } catch (IOException e) { e.printStackTrace(); } ``` 在此示例中,我们打开名为“file.txt”的文件,并使用Java的FileOutputStream类将字符串“Hello World”写入文件。我们使用Context.MODE_PRIVATE参数指定文件的访问权限为私有,这意味着只有我们的应用程序可以访问该文件。 总结 Android应用程序的私有目录提供了一个安全的存储区域,只有应用程序本身才能访问该目录中的文件。通过使用Context对象提供的openFileInput()和openFileOutput()方法,我们可以读取和写入应用程序的私有目录中的文件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值