【问题标题】:How to create ".nomedia" file and store images inside it Programmatically?如何以编程方式创建“.nomedia”文件并将图像存储在其中?
【发布时间】:2018-12-21 07:35:52
【问题描述】:

对于我的应用程序,图像存储在我手机的内部存储器中,并且图像在图库中可见,但我的客户希望图像在图库中不可见。

我在存储图像的文件夹中手动添加了 .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;
}

【问题讨论】:

  • 在设置点(。)之前以编程方式创建文件夹
  • @Pratik18 谢谢,但是如果您共享用于创建 .nomedia 文件的代码,或者我如何在“Android”文件夹中创建一个文件夹(其中存在数据和 obb 文件夹),那么画廊将忽略它

标签: java android android-gallery


【解决方案1】:

在存储中创建 .nomedia 文件以隐藏媒体播放器中的音频:

String filepath = Environment.getExternalStorageDirectory().getPath()+"/";

File file = new File(filepath+".nomedia");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}

【讨论】:

  • 为我工作,用相机拍摄后图库中不再显示照片。
【解决方案2】:
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();
            }

Android/data/你的包名/files/这是你的Android数据文件夹路径,你可以在这个文件夹中保存图片。

【讨论】:

  • 我应该传递从 activityResult 方法获得的图像 Uri:getExternalFilesDir(Uri);
  • 这里:文件路径 = new File(root, "dump.png");我应该使用从 startActivityResult 方法获得的图像 Uri 而不是 dump.png。正确的? @Patrik18
  • dump.png 是图像名称。您可以根据需要更改。
  • 你必须已经转换位图在我的代码中使用这个位图。
  • 好的,但是我从 onActivityResult() 中的 startActivityResult 得到的 Uri 呢,看看我的代码@Pratik18
猜你喜欢
  • 2015-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
  • 2020-09-12
  • 2023-02-23
相关资源
最近更新 更多