Android 圖片保存到相冊(cè)時(shí)間顯示為1970的問(wèn)題,

第一步:先申請(qǐng)讀寫權(quán)限(讀寫權(quán)限都要申請(qǐng))

  String base64 = (String) msg.obj;
                    bmp = ImageUtils.base64ToBitmap(base64);
                    if (Build.VERSION.SDK_INT >= M) {
                        if (ContextCompat.checkSelfPermission(WebViewActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager
                                .PERMISSION_GRANTED || ContextCompat.checkSelfPermission(WebViewActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager
                                .PERMISSION_GRANTED) {
                            //進(jìn)行權(quán)限請(qǐng)求
                            ActivityCompat.requestPermissions(WebViewActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, SAVE_BITMAP);
                        } else {
                            saveImageToGallery(bmp);
                        }
                    } else {
                        saveImageToGallery(bmp);
                    }



   /**
     * 圖片保存相冊(cè)方法
     *
     * @param bitmap
     */

    public void saveImageToGallery(Bitmap bitmap) {
        // 首先保存圖片
        File file = null;
        String fileName = System.currentTimeMillis() + ".jpg";
        File root = new File(getExternalCacheDir(), getPackageName());
        if (!root.exists()) {
            root.mkdir();
        }
        file = new File(root, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        //其次把文件插入到系統(tǒng)圖庫(kù)
        try {
            String insertImage = MediaStore.Images.Media.insertImage(this.getContentResolver(), file.getAbsolutePath(), fileName, null);
            File file1 = new File(getRealPathFromURI(Uri.parse(insertImage),WebViewActivity.this));
            updatePhotoMedia(file1,this);
            Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
    //更新圖庫(kù)
    private  void updatePhotoMedia(File file ,Context context){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(file));
        context.sendBroadcast(intent);
    }
    //得到絕對(duì)地址
    private static String getRealPathFromURI(Uri contentUri,Context context) {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String fileStr = cursor.getString(column_index);
        cursor.close();
        return fileStr;
    }








//kotlin語(yǔ)法
fun saveImageToGallery(bitmap: Bitmap) {
        // 首先保存圖片
        var file: File? = null
        val fileName = (System.currentTimeMillis()).toString() + ".jpg"
        val root = File(externalCacheDir, packageName)
        if (!root.exists()) {
            root.mkdir()
        }
        file = File(root, fileName)
        try {
            val fos = FileOutputStream(file!!)
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)
            fos.flush()
            fos.close()
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        //其次把文件插入到系統(tǒng)圖庫(kù)
        try {
            val insertImage = MediaStore.Images.Media.insertImage(this.contentResolver, file!!.absolutePath, fileName, null)
            val file1 = File(getRealPathFromURI(Uri.parse(insertImage), this))
            updatePhotoMedia(file1,this);

        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        }

    }
  //更新圖庫(kù)
    fun updatePhotoMedia(file: File, context: Context) {
        val intent = Intent()
        intent.action = Intent.ACTION_MEDIA_SCANNER_SCAN_FILE
        intent.data = Uri.fromFile(file)
        context.sendBroadcast(intent)
    }

    //得到絕對(duì)地址
    fun getRealPathFromURI(contentUri: Uri, context: Context): String {
        val proj = arrayOf(MediaStore.Images.Media.DATA)
        val cursor = context.getContentResolver().query(contentUri, proj, null, null, null)
        val column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
        cursor.moveToFirst()
        val fileStr = cursor.getString(column_index)
        cursor.close()
        return fileStr
    }

okay

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容