Ajay Gohel
Ajay Gohel

Reputation: 243

How to create folder in internal storage and save captured image

I want to capture image and save it to specific folder in internal storage. Currently i am able to open intent and get thumbnail of captured image. I dont want to user extrnal stotage as now mostly users use their internal storage and not sd card.

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null){
    startActivityForResult(intent,1);
}


  @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK){
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");

        LayoutInflater inflater = LayoutInflater.from(LeaveApplicationCreate.this);
        final View view = inflater.inflate(R.layout.item_image,attachView, false);

        ImageView img = view.findViewById(R.id.img);
        AppCompatImageView btnRemove = view.findViewById(R.id.btnRemove);
        img.setImageBitmap(imageBitmap);

        btnRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                attachView.removeView(view);
            }
        });

        attachView.addView(view);

        File directory = new File(Environment.getExternalStorageDirectory(),"/Digimkey/Camera/");
        if (!directory.exists()) {
            directory.mkdir();
        }
        File file = new File(directory, System.currentTimeMillis()+".jpg");


        try (FileOutputStream out =new FileOutputStream(file)) {
            imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  }

Upvotes: 0

Views: 1124

Answers (2)

Ashok Reddy M
Ashok Reddy M

Reputation: 330

Use to method to save your bimap in local storage. Pass bimap image as parameter i.e saveToInternalStorage(imageBitmap)

private String saveToInternalStorage(Bitmap bitmapImage){
    //set image saved path 
    File storageDir = new File(Environment.getExternalStorageDirectory()
        + "MyApp"+ "/Files");

      if (!storageDir.exists()) {
        storageDir.mkdirs();
      }
    File mypath=new File(storageDir,"bitmap_image.jpg");
    FileOutputStream fos = null;
    try {           
        fos = new FileOutputStream(mypath);
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
    } catch (Exception e) {
          e.printStackTrace();
    } finally {
        try {
          fos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
    } 
    return directory.getAbsolutePath();
}

Required permissions in Manifest:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Upvotes: 0

kashyap
kashyap

Reputation: 508

First gain Write Permissions.

File directory = new File(Environment.getExternalStorageDirectory(), dirName);
        if (!directory.exists()) {
            directory.mkdirs();
          }
        File file = new File(directory, fileName);
         if (!file.exists()) {
          file.createNewFile();
        }

try (FileOutputStream out =new FileOutputStream(file)) {
bmp.compress(Bitmap.CompressFormat.PNG, 100, out); 
 } catch (IOException e) {
    e.printStackTrace();
 }

There are two types of storage. 1) Internal ex. "/root/.." Unless you have rooted device, we can't access. this path. 2) External ex. "/storage/emuated/0" Environment.getExternalStorageDirectory() By using this path, we are able to create a directory/file.

Upvotes: 1

Related Questions