Я хочу сохранить объект Image (не рисуемый) из внешнего хранилища. Как? - PullRequest
0 голосов
/ 16 апреля 2019

У меня есть объект 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 голосов
/ 16 апреля 2019

Вы можете выбрать изображение с камеры или галереи и сохранить его локально во внешнем хранилище. { 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 "";
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...