2017-08-18 115 views
0

在下面的代码中,您可以看到我已经使用位图创建了一个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(); 
    } 
}); 
} 
+0

照片比预期的更难,因为任何人都会认为这应该是微不足道的,但实际上这并不是很长而且乏味。我建议你这个图书馆github.com/fabian7593/MagicalCamera是最好的拍照,照顾权限,拍照或从画廊中选择它,它将它保存为设备中的文件,它也会注册它在画廊中,有另一个功能,但应该让你开始 – cutiko

回答

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" /> 
+0

感谢您的回应!我相信它会工作,但我的模拟设备是说我的虚拟SD卡已损坏。你知道如何解决这个问题吗? –

+0

尝试创建一个新的AVD,因为SD卡映像似乎存在一些问题。但是,我建议您在真实设备而不是仿真器上进行测试。 – Abhi

+0

我创建了一个新的AVD,它不工作。稍后我会尝试使用设备!谢谢! –

0

如果你正在寻找一些代码,这将有助于你从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将负责在图库中扫描图像。 希望这有助于。

相关问题