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

问题描述 投票:-1回答:1

我有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
 }

我想存储帧不是空的图像。请解释处理此图像对象的正确方法,并将图像保存在外部存储器中。

android image api file android-external-storage
1个回答
0
投票

您可以从相机或图库中选择图像,并在本地存储在外部存储中。 {public void choosePhotoFromGallary(){

   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 "";
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.