将相机图像保存到应用程序包文件夹

Posted

技术标签:

【中文标题】将相机图像保存到应用程序包文件夹【英文标题】:Save Camera Image to App Package Folder 【发布时间】:2018-04-01 07:32:06 【问题描述】:

我正在尝试将相机图像直接存储在我的应用程序包名称下。我已经阅读了其他类似的问题,并完全按照本教程here 进行操作。

我的示例代码:

public class MainActivity extends AppCompatActivity 

private ImageView imageView;
private Button btnCamera, btnGallery;
private File file;
private Uri uri;

private DisplayMetrics displayMetrics;
int width, height;

public static final int CAM_CODE = 101;
public static final int GALL_CODE = 108;

public static String mCurrentPhotoPath;
public static final String Package_Name = "com.joey.example.camerasqlite";

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageView = (ImageView) findViewById(R.id.imageView);
    btnCamera = (Button) findViewById(R.id.btnCamera);
    btnGallery = (Button) findViewById(R.id.btnGallery);

    btnCamera.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            launchCamera();
        
    );

    btnGallery.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            pickGallery();
        
    );



public File createImageFile() throws IOException 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String fileName =  timeStamp + "_";

    File storageDir = getExternalFilesDir(null);
    //File storageDir = getFilesDir();
    File image = File.createTempFile(fileName, ".png", storageDir);

    mCurrentPhotoPath = image.getAbsolutePath();

    return image;



private void launchCamera() 
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (cameraIntent.resolveActivity(getPackageManager()) != null)

        File photoFile = null;
        try 
            photoFile = createImageFile();
         catch (IOException e) 
            e.printStackTrace();
        

        if (photoFile != null)
            Uri photoUri = FileProvider.getUriForFile(this, Package_Name, photoFile);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
            cameraIntent.putExtra("return-data", true);
            startActivityForResult(cameraIntent, CAM_CODE);
        
    


private void pickGallery() 
    Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
    galleryIntent.setType("image/*");
    startActivityForResult(galleryIntent, GALL_CODE);


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAM_CODE && resultCode == RESULT_OK)
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(bitmap);
    

    if (requestCode == GALL_CODE && resultCode == RESULT_OK)
        uri = data.getData();
        imageView.setImageURI(uri);
    


应用程序在运行时崩溃,并在 logcat 中显示此消息:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/android/data/com.joey.example.camerasqlite/files/20171020_115905_1523486524.png
                                                   at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
                                                   at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
                                                   at com.joey.example.camerasqlite.MainActivity.launchCamera(MainActivity.java:88)
                                                   at com.joey.example.camerasqlite.MainActivity.access$000(MainActivity.java:20)
                                                   at com.joey.example.camerasqlite.MainActivity$1.onClick(MainActivity.java:48)
                                                   at android.view.View.performClick(View.java:5690)
                                                   at android.view.View$PerformClick.run(View.java:22693)
                                                   at android.os.Handler.handleCallback(Handler.java:836)
                                                   at android.os.Handler.dispatchMessage(Handler.java:103)
                                                   at android.os.Looper.loop(Looper.java:203)
                                                   at android.app.ActivityThread.main(ActivityThread.java:6269)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

这是我与文件提供者的清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
    android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera2"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <provider
        android:authorities="com.joey.example.camerasqlite"
        android:name="android.support.v4.content.FileProvider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"/>
    </provider>
</application>

已在此处添加 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path name="my_images" path="Android/data/com.joey.example.camerasqlite/files/Pictures"/>
    <!--<files-path name="my_images" path="Android/data/com.joey.example.camerasqlite/files/Pictures"/>-->
</paths>

我需要一些帮助来解决这个问题。谢谢

【问题讨论】:

上传你的file_paths.xml文件的代码。 为什么要硬编码 Package_Name?你不知道函数 getPackageName() 吗? 而且你不需要那个变量或那个函数。您可以只使用 getExternalFilesDir()。 我们使用 getFilesDir() 而不是 getExternalFilesDir() 进行内部存储。要保存在外部存储中,您需要获得许可。但 getFilesDir() 不需要许可。尝试将其更改为,看看它是否有效,并告诉我您是否需要权限或什么? 使用 Environment.getExternalStorageDirectory()+"你的文件夹名"+"文件名.jpg" 【参考方案1】:

变化:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path name="my_images" path="Android/data/com.joey.example.camerasqlite/files/Pictures"/>
    <!--<files-path name="my_images" path="Android/data/com.joey.example.camerasqlite/files/Pictures"/>-->
</paths>

与:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path name="my_images" path="."/>
    <!--<files-path name="my_images" path="."/>-->
</paths>

【讨论】:

以上是关于将相机图像保存到应用程序包文件夹的主要内容,如果未能解决你的问题,请参考以下文章

如何将图像从相机保存到 iPhone 画廊中的特定文件夹?

将相机捕获的图像保存到ios中的文档目录中[重复]

Android将相机图像保存在自定义文件夹中

将点击的图像保存到自定义文件夹(最好是应用程序内部)而不是图库

将图像保存到特定相册,但限制保存到相机胶卷文件夹

将图像从相机保存到数据库[重复]