【问题标题】:How to save Image(imported from gallery of phone to android app) and save into android app local directory?如何保存图像(从手机图库导入到 android 应用程序)并保存到 android 应用程序本地目录?
【发布时间】:2013-08-21 12:10:50
【问题描述】:

我已经搜索了与此相关的所有问题,但我没有得到解决方案。我的要求是从我的 Android 应用程序中,用户可以从手机图库中选择图像并将其设置为他的最爱。我的问题是从图库中拍摄的照片需要保存到应用程序本地目录(该目录对用户不可见,因为它将存储在应用程序中)。如果用户从手机图库中删除图像,那么应用程序也应该显示他最喜欢的图像。所以我需要保存本地目录。请帮助我。提前致谢。我知道以下代码用于存储到 sdcard 但我需要将图像从 sdcard 保存到应用程序的本地文件夹。

  BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;
        Bitmap bm = LoadImage(imagePath, bmOptions);
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/MyFolder");    
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-"+ n +".jpg";
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete (); 
        try {
               FileOutputStream out = new FileOutputStream(file);
               bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
               out.flush();
               out.close();

        } catch (Exception e) {
               e.printStackTrace();
        }

【问题讨论】:

    标签: android image android-sdcard


    【解决方案1】:

    使用例如:

     File path = getExternalFilesDir(Environment.DIRECTORY_DCIM);
           File image = new File(path,imageFileName );
    

    您的图片将保存在您的应用目录中。

    【讨论】:

      猜你喜欢
      • 2016-01-26
      • 2022-06-19
      • 1970-01-01
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      相关资源
      最近更新 更多