我想保存外部存储的Image对象(不可绘制)。

时间:2019-04-16 05:41:37

标签: android image api file android-external-storage

我有Image对象,并且将其byteBuffer保存到Bitmap中以下载图像。但是我不认为我正在正确地创建位图,因为正在创建0Byte图像。

    I have looked everywhere how to convert the image object(not the drawable) to convert to bitmap.But i cant find a valid way.      

    //arcore image acquire.
   Image image=frame.acquireCameraImage();
   //edgeDetector is a function that returns these values wrapped 
   //into a bytebuffer. 
    ByteBuffer buffer11=edgeDetector.detect(
                                        image.getWidth(),
                                        image.getHeight                            (),
                                        image.getPlanes()[0].getRowStride(),
                                        image.getPlanes()[0].getBuffer());

                  byte[] bytes = new byte[buffer11.capacity()];
                                buffer11.get(bytes);
                                Log.i("ByteBuffer",bytes.toString());
       Bitmap bitmapImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);
                                Log.i("Bitmap","created");
                                Date currentTime11 = Calendar.getInstance().getTime();
         String root = Environment.getExternalStorageDirectory().toString();
                      File myDir = new File(root + "/saved_images");
                         if (!myDir.exists()) {
                                    myDir.mkdirs();
                                }
     String myimage = "I-"+ currentTime11.toString() +".jpg";
     File file = new File (myDir, myimage);
     if (file.exists ())
       file.delete ();

      try {
        FileOutputStream out = new FileOutputStream(file);
                Log.i("Checking","Checking");
                if(bitmapImage!=null)
                {

        bitmapImage.compress(Bitmap.CompressFormat.JPEG, 90, out);
        //saving the bitmap.

}

    out.flush();
    out.close();
    image.close();//closing image so the application wont crash.
}


 catch (Exception e) {
       Log.i("Error","Try again");
       //handling exceptions
 }

我要存储框架获取的图像,这些图像不为空。 请说明处理此图像对象并将图像保存在外部存储器中的正确方法。

1 个答案:

答案 0 :(得分:0)

您可以从相机或图库中选择图像,然后本地存储在您的外部存储中。 { 公共无效的selectPhotoFromGallary(){

   Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    startActivityForResult(galleryIntent, GALLERY);
}

private void takePhotoFromCamera() {
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, CAMERA);
}

public String saveImage(Bitmap myBitmap) {

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
        File wallpaperDirectory = new File(
                Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);
        // have the object build the directory structure, if needed.
        if (!wallpaperDirectory.exists()) {
            wallpaperDirectory.mkdirs();
        }

        try {
            File f = new File(wallpaperDirectory, Calendar.getInstance()
                    .getTimeInMillis() + ".jpg");
            f.createNewFile();
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
            MediaScannerConnection.scanFile(this,
                    new String[]{f.getPath()},
                    new String[]{"image/jpeg"}, null);
            fo.close();
            Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());

            return f.getAbsolutePath();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return "";
    }
}