【问题标题】:I want to save the Image object(not drawable) of external storage.How?我想保存外部存储的图像对象(不可绘制)。如何?
【发布时间】:2019-04-16 05:41:36
【问题描述】:

我有 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】:

您可以从相机或图库中选择图像并本地存储在您的外部存储中。 { 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 "";
    }
}

【讨论】:

  • 这适用于位图。我的问题是我有 android.media.Image 并且我没有找到将其转换为位图的方法。
猜你喜欢
  • 2015-09-24
  • 2018-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
  • 2018-01-30
  • 2018-04-20
相关资源
最近更新 更多