如何以编程方式创建“ .nomedia”文件并将图像存储在其中?

时间:2018-12-21 07:35:53

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

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

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 /数据/您的包名称/文件/,这是您可以在其中保存图像之后的Android数据文件夹路径。