【问题标题】:Cannot save image from Bitmap无法从位图中保存图像
【发布时间】:2017-02-05 02:51:21
【问题描述】:

我收到一条警告,指出 cachePath.createNewFile(); 的结果被忽略。否则,以下代码不会将图像保存到我的手机中。我能做些什么?

holder.messageImage.setOnLongClickListener(v -> {
            v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);

            Bitmap bitmap = ((BitmapDrawable) holder.messageImage.getDrawable()).getBitmap();

            File root = Environment.getExternalStorageDirectory();
            File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");

            try {
                FileOutputStream ostream = new FileOutputStream(cachePath);
                bitmap.compress(CompressFormat.JPEG, 100, ostream);
                ostream.close();
                Toast.makeText(mContext, "Image saved successfully", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
                Log.w(getClass().toString(), e);
                Toast.makeText(mContext, "Failed saving image", Toast.LENGTH_SHORT).show();
            }

            return false;
        });

我以这种方式从后端下载图像:

private void downloadMessageImage(ViewHolder holder, int position) {
        ParseQuery<ParseObject> query = new ParseQuery<>(ParseConstants.CLASS_YEET);
        query.whereEqualTo(ParseConstants.KEY_OBJECT_ID, mYeets.get(position).getObjectId());
        query.findInBackground((user, e) -> {
            if (e == null) for (ParseObject userObject : user) {

                if (userObject.getParseFile("image") != null) {
                    String imageURL = userObject.getParseFile("image").getUrl();
                    /*Log.w(getClass().toString(), imageURL);*/

                   if (imageURL != null) {

                        holder.messageImage.setVisibility(View.VISIBLE);

                        Picasso.with(mContext)
                                .load(imageURL)
                                .placeholder(R.color.placeholderblue)
                                .into(holder.messageImage);

                    } else {
                        holder.messageImage.setVisibility(View.GONE);
                    }
                }

            }
        });
    }

位图肯定不存在:android.graphics.Bitmap@12d9cc4

【问题讨论】:

  • cachePath.createNewFile(); 删除该声明。没用。
  • 你有什么例外吗?如果是,请发布 logcat。
  • 也不例外。
  • 在那个 catch 块中放一个 Toast。如果有,您应该通知用户。
  • following code does not save an image to my phone。你是怎么检查的?

标签: java android bitmap fileoutputstream bitmapdrawable


【解决方案1】:

要保存图像,我使用以下代码:

try {
       signature.setDrawingCacheEnabled(true);
       Bitmap bm = Bitmap.createBitmap(signature.getDrawingCache());
       // Define params for save
       File f = new File(Environment.getExternalStorageDirectory() + "/Cassiopea/momomorez/" + File.separator + "signature.png");
       f.createNewFile();
       FileOutputStream os = new FileOutputStream(f);
       os = new FileOutputStream(f);
       //compress to specified format (PNG), quality - which is ignored for PNG, and out stream
       bm.compress(Bitmap.CompressFormat.PNG, 100, os);
       Toast.makeText(mContext, "Saving image OK", Toast.LENGTH_SHORT).show();
       os.close();
} 
catch (Exception e) {
       Log.v("Gestures", e.getMessage());
       e.printStackTrace();
}

使用此代码将图案保存在已建立文件夹中的图像中。

【讨论】:

  • 这不起作用。 Saving image OK toast 已显示,但我在手机上的任何地方都找不到该图像。
  • 是一个 GestureOverlayView 我用它来创建一个平板制作的签名图片
  • 在 AndroidManifest.xml 中存在
猜你喜欢
  • 2016-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多