【问题标题】:How to create folder in internal storage and save captured image如何在内部存储中创建文件夹并保存捕获的图像
【发布时间】:2019-09-21 21:59:31
【问题描述】:

我想捕获图像并将其保存到内部存储中的特定文件夹。目前我能够打开意图并获取捕获图像的缩略图。我不想使用外部存储,因为现在大多数用户使用他们的内部存储而不是 sd 卡。

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();
        }
    }
  }

【问题讨论】:

  • 既然你已经成功获取了图片,你可以直接使用OpenFileOutput(带有Seri​​alizable接口)将它保存到内部存储中。
  • “我不想使用外部存储,因为现在大多数用户使用的是内部存储而不是 sd 卡” -- external storage,从 Android SDK 的角度来看,不是 removable storage。用户无法直接访问 Android SDK 调用的 internal storage

标签: android android-intent camera


【解决方案1】:

首先获得写入权限。

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();
 }

有两种类型的存储。 1)内部前。 “/根/..” 除非您有root设备,否则我们无法访问。这条路。 2)外部前。 “/存储/仿真/0” Environment.getExternalStorageDirectory() 通过使用这个路径,我们可以创建一个目录/文件。

【讨论】:

  • 但是要在内部存储中创建文件夹并将图像保存在其中?
  • 更新了答案
  • 但它会保存在外部存储而不是内部存储中
  • 在应用缓存中的意思?
  • 好的,我明白了,我的 cncept 错了。我认为 getExternalDirectory 是 sdcard
【解决方案2】:

使用 to 方法将您的 bimap 保存在本地存储中。将 bimap 图像作为参数传递 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();
}

Manifest 中的必需权限:

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

【讨论】:

  • 这个特定的存储目录可以通过File storageDir = getExternalFilesDir(null);访问
  • 是的,您可以使用 getExternalFilesDirs(),它返回一个包含每个存储位置条目的文件数组。
  • 无法在内部存储中创建文件夹,如 WhatsApp、SnapChat 创建?
  • 是的,您可以使用@kashyap 回答。 dirName 将是 SnapChatWhatsapp 或任何你想要的
猜你喜欢
  • 1970-01-01
  • 2017-12-11
  • 2014-05-16
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
相关资源
最近更新 更多