如何将创建的图像视图保存到相机胶卷?

时间:2017-08-18 14:32:38

标签: android qr-code

在下面的代码中,您可以看到我使用位图创建了一个imageview。我想知道的是如何将该imageview的图像保存到我的相机胶卷中。谢谢!

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.key_code_zoom);

title = (TextView) findViewById(R.id.accountTitleLarge);
imageView = (ImageView) findViewById(R.id.keyCodeLarge);

Intent callingActivity = getIntent();
Bundle callingBundle = callingActivity.getExtras();
if (callingBundle != null) {
    String titleText = callingBundle.getString("title");
    byte[] bytes = getIntent().getByteArrayExtra("bitmapbytes");
    bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    title.setText(titleText);
    imageView.setImageBitmap(bmp);
}

imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        supportFinishAfterTransition();
    }
});
}

2 个答案:

答案 0 :(得分:0)

如果您正在寻找一些可以帮助您将Image从ImageView保存到手机存储的代码,那么以下代码可以帮助您

 public String getImageFromView() {

    imageview.setDrawingCacheEnabled(true);
    // this is the important code :)
    // Without it the view will have a dimension of 0,0 and the bitmap will be null
    imageview.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 

    imageview.layout(0, 0, imageview.getMeasuredWidth(), imageview.getMeasuredHeight());
    imageview.buildDrawingCache(true);

    //Define a bitmap with the same size as the view
    Bitmap b = imageview.getDrawingCache();
    String imgPath = getImageFilename();
    File file = new File(imgPath);

    try {
        OutputStream os = new FileOutputStream(file);
        b.compress(Bitmap.CompressFormat.JPEG, 90, os);
        os.flush();
        os.close();
        ContentValues image = new ContentValues();
        image.put(Images.Media.TITLE, "NST");
        image.put(Images.Media.DISPLAY_NAME, imgPath.substring(imgPath.lastIndexOf('/')+1));
        image.put(Images.Media.DESCRIPTION, "App Image");
        image.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
        image.put(Images.Media.MIME_TYPE, "image/jpg");
        image.put(Images.Media.ORIENTATION, 0);
        File parent = file.getParentFile();
        image.put(Images.ImageColumns.BUCKET_ID, parent.toString()
                .toLowerCase().hashCode());
        image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, parent.getName()
                .toLowerCase());
        image.put(Images.Media.SIZE, file.length());
        image.put(Images.Media.DATA, file.getAbsolutePath());
        Uri result = mContext.getContentResolver().insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);
    } catch (FileNotFoundException fnfe) {
        fnfe.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return imgPath;
    }

您必须提供文件路径getImageFilename();此函数提供了存储文件的路径。 ContentValues将负责让图片在图库中进行扫描。 希望这会有所帮助。

答案 1 :(得分:0)

要在图库中保存图像,必须先获取位图然后保存。

private void imageToRoll(){
imageView.buildDrawingCache();
Bitmap image = imageView.getDrawingCache();  // Gets the Bitmap
MediaStore.Images.Media.insertImage(getContentResolver(), imageBitmap, imagTitle , imageDescription);  // Saves the image.
}

另外,在清单中设置权限。

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