【问题标题】:Failed to save image from app assets folder to Gallery Folder in Android?无法将图像从应用资产文件夹保存到 Android 中的图库文件夹?
【发布时间】:2014-02-22 08:04:05
【问题描述】:

我已经编写了这段代码,它在指定文件夹名称中保存图像,但我想将此图像保存在 Gallery/Specified FolderName/Image中。有人可以帮我吗..... 代码:

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();
    }
}

这里我使用了另一个函数从资产文件夹中获取图像。请让我知道在此代码中添加什么以将其保存在 Gallery/FolderName/Image 中

【问题讨论】:

  • logcat 中有任何堆栈跟踪吗?
  • 您是否为清单文件添加了读写外部存储权限?

标签: android imageview android-assets


【解决方案1】:

添加权限到 mainfest.xml 文件

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

这是一个link检查这个

【讨论】:

  • 是的,我已经添加了如何使此代码在图库中创建文件夹并立即将图像保存在文件中的创建文件夹
  • 这个链接可以帮助你mybringback.com/tutorial-series/12696/…
  • 你能帮我在成功保存图像后如何向用户显示警报
  • 保存图片后使用Toast显示消息。
  • 保存后使用此代码。 Toast.makeText(getApplicationContext(), "图片保存成功",Toast.LENGTH_LONG).show();
猜你喜欢
  • 2022-11-20
  • 2015-02-06
  • 1970-01-01
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多