Android - Save images in an specific folder

后端 未结 5 1489
执笔经年
执笔经年 2020-11-29 22:01

I need to save the pictures taken with my app in an specific folder. I\'ve read many solutions to this problem but I couldn\'t make any of them work so I ask for help.

5条回答
  •  自闭症患者
    2020-11-29 22:15

    Here You Go. I tried the above solution they save the image to gallery but the image is not visible , a 404 error is visible on the image , and i figured it out .

    public void addToFav(String dirName, Bitmap bitmap) {
    
        String resultPath = getExternalFilesDir(Environment.DIRECTORY_PICTURES)+
                dirName + System.currentTimeMillis() + ".jpg";
        Log.e("resultpath",resultPath);
        new File(resultPath).getParentFile().mkdir();
    
    
    
    
        if (Build.VERSION.SDK_INT < 29){
            
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, "Photo");
            values.put(MediaStore.Images.Media.DESCRIPTION, "Edited");
            values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
            values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
            values.put("_data", resultPath);
    
            ContentResolver cr = getContentResolver();
            cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    
            try {
                OutputStream fileOutputStream = new FileOutputStream(resultPath);
                bitmap.compress(CompressFormat.JPEG, 100, fileOutputStream);
                fileOutputStream.flush();
                fileOutputStream.close();
                if(fileOutputStream != null){
                    Toast.makeText(this, "Image Saved", Toast.LENGTH_SHORT).show();
                }
            } catch (IOException e2) {
                e2.printStackTrace();
            }
    
        }else {
    
            OutputStream fos = null;
            File file = new File(resultPath);
    
            final String relativeLocation = Environment.DIRECTORY_PICTURES;
            final ContentValues  contentValues = new ContentValues();
    
            contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation+"/"+dirName);
            contentValues.put(MediaStore.MediaColumns.TITLE, "Photo");
            contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
            contentValues.put(MediaStore.MediaColumns.DATE_TAKEN, System.currentTimeMillis ());
            contentValues.put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis());
            contentValues.put(MediaStore.MediaColumns.BUCKET_ID, file.toString().toLowerCase(Locale.US).hashCode());
            contentValues.put(MediaStore.MediaColumns.BUCKET_DISPLAY_NAME, file.getName().toLowerCase(Locale.US));
            
            final ContentResolver resolver = getContentResolver();
            final Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            Uri uri = resolver.insert(contentUri, contentValues);
    
            try {
                fos = resolver.openOutputStream(uri);
                bitmap.compress(CompressFormat.JPEG, 100, fos);
                fos.flush();
                fos.close();
    
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(fos != null){
                Toast.makeText(this, "Image Saved", Toast.LENGTH_SHORT).show();
            }
    
        }
    
    
    }
    

提交回复
热议问题