我正在尝试将相机图像直接存储在我的应用程序包名称下。我已阅读其他类似问题,并完全按照本教程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>
我需要一些帮助来解决这个问题。感谢
答案 0 :(得分:0)
Change:
<?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>
with:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path name="my_images" path="."/>
<!--<files-path name="my_images" path="."/>-->
</paths>