Failed to save image from app assets folder to Gallery Folder in Android?

半腔热情 提交于 2019-12-13 20:50:54

问题


I have written this code its saving image in specified folder name but i want to save this image in Gallery/Specified FolderName/Image.Can someone please help me ..... CODE:

saveImageView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            saveImage(getBitmapFromAsset("save_fatwa.jpg"),
                    "save_fatwa.jpg");
        }
    });


private Bitmap getBitmapFromAsset(String strName) {
    AssetManager assetManager = getAssets();
    InputStream istr = null;
    Bitmap bitmap = null;
    try {
        istr = assetManager.open(strName);
        if (istr.equals(null)) {
            Log.i("getBitmapFromAsset isStr", "" + istr);
            bitmap = BitmapFactory.decodeStream(assetManager
                    .open("save_fatwa.jpg"));
        } else {
            bitmap = BitmapFactory.decodeStream(istr);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    // Bitmap bitmap = BitmapFactory.decodeStream(istr);
    return bitmap;
}

private void saveImage(Bitmap finalBitmap, String imageName) {
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/Jamia Binoria Images");
    myDir.mkdirs();

    // String fname = "save_fatwa.jpg";
    File file = new File(myDir, imageName);
    if (file.exists()) {
        Log.i("file exists", "" + imageName);
        file.delete();
    } else {
        Log.i("file does not exists", "" + imageName);
    }
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG,90,out);
        // sendBroadcast(new Intent(
        // Intent.ACTION_MEDIA_MOUNTED,
        // Uri.parse("file://" +
        // Environment.getExternalStorageDirectory())));
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Here i have used another function to get images from Assets Folder.Please let me know what to add in this code to save it in Gallery/FolderName/Image


回答1:


Add Permission to mainfest.xml file

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

Here is a link check this



来源:https://stackoverflow.com/questions/21951558/failed-to-save-image-from-app-assets-folder-to-gallery-folder-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!