对于我的应用程序,图像存储在手机的内部存储器中,图像在图库中可见,但我的客户希望图像在图库中不可见。
我在存储图像的文件夹中手动添加了 .nomedia 文件,它消失了,但我再次拍摄了新图像,它在图库中可见。
那么我怎样才能以编程方式做到这一点,这样图像就不会出现在我的画廊中呢?
这是我的代码。
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode,data);
if(resultCode== Activity.RESULT_OK){
if(requestCode==REQUEST_CAMERA){
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
// Get the path from the Uri
String path = getPathFromURI(selectedImageUri);
File file = new File(path);
Bitmap bmp = CommonMethod.compressImage(file, getContext());
Log.e(TAG, "onActivityResult --: "+ String.format("Size : %s", getReadableFileSize(file.length())));
mCustomerImage = CommonMethod.bitmapToByteArray(bmp);
imageTemplateStr = Base64.encodeToString(mCustomerImage, Base64.DEFAULT);
Log.e(TAG, "image: "+ imageTemplateStr );
imageCustomer.setImageBitmap(bmp);
}
}else if(requestCode==SELECT_FILE){
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
// Get the path from the Uri
String path = getPathFromURI(selectedImageUri);
File file = new File(path);
Bitmap bmp = CommonMethod.compressImage(file, getContext());
Log.e(TAG, "onActivityResult --: "+ String.format("Size : %s", getReadableFileSize(file.length())));
mCustomerImage = CommonMethod.bitmapToByteArray(bmp);
imageTemplateStr = Base64.encodeToString(mCustomerImage, Base64.DEFAULT);
Log.e(TAG, "image: "+ imageTemplateStr );
imageCustomer.setImageBitmap(bmp);
}
}
getPathFromUri 方法
public String getPathFromURI(Uri contentUri) {
String res = null;
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(contentUri, proj, null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
compressImage() 方法。
public static Bitmap compressImage(File imgFile, Context context) {
Bitmap compressedImgBitmap = new Compressor.Builder(context)
.setMaxWidth(640)
.setMaxHeight(480)
.setCompressFormat(Bitmap.CompressFormat.PNG)
.build()
.compressToBitmap(imgFile);
return compressedImgBitmap;
}
最佳答案
在存储中创建 .nomedia 文件以从媒体播放器隐藏音频:
String filepath = Environment.getExternalStorageDirectory().getPath()+"/";
File file = new File(filepath+".nomedia");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
关于java - 如何以编程方式创建 ".nomedia"文件并在其中存储图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53880855/