无法从位图保存图像

时间:2016-09-27 12:53:12

标签: java android bitmap fileoutputstream bitmapdrawable

我收到一条警告,指出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

1 个答案:

答案 0 :(得分:0)

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

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

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

相关问题