gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:36:20 24 4
gpt4 key购买 nike

我试图将相机图像直接存储在我的应用程序包名称下。我已经阅读了其他类似的问题并遵循了本教程 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)

这是我与文件提供者的 list :

<?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>

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

最佳答案

改变:

<?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>

关于android - 将相机图像保存到应用程序包文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46843827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com