java - 如何以编程方式创建 ".nomedia"文件并在其中存储图像?

标签 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);
        }

}

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/

相关文章:

java - 将罗马数字转换为整数值?

java - 为什么在 Java (BlueJ) 中使用 ArrayList 作为构造函数参数?

java - 未从具有 Room 的数据库中选择数据

android - 滚动图库启用按下状态并从子项中删除点击监听器

java - jTable 将数据输入到列中

java - 从 java 调用 HTPS WebService

java - Android 代码调用或调用包含字母 *123*abc# 而不是数字 (*123#) 的字符串?

android - AsyncTask 和 fragment 异常 android.view.WindowManager$BadTokenException

android - 获取当前图片在图库中显示的位置

android - ActivityForResult 只获取 JPEG