Ras*_*oro 5 java android android-gallery
对于我的应用程序,图像存储在我手机的内部存储器中,图像在图库中可见,但我的客户希望图像在图库中不可见。
我在存储图像的文件夹中手动添加了 .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);
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
在存储中创建 .nomedia 文件以隐藏媒体播放器中的音频:
String filepath = Environment.getExternalStorageDirectory().getPath()+"/";
File file = new File(filepath+".nomedia");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
File files = getExternalFilesDir("");
File root = new File(files.getAbsolutePath()+"/Saveimags");
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, ".nomedia");
FileWriter writer = new FileWriter(gpxfile);
writer.flush();
writer.close();
File path = new File(root, "dump.png");
try {
FileOutputStream out = new FileOutputStream(path);
Bitmap myBitmap = BitmapFactory.decodeFile(selectedImagePath);
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
Android/data/你的包名/files/ 这是你保存图片后的Android数据文件夹路径。
| 归档时间: |
|
| 查看次数: |
3219 次 |
| 最近记录: |