在外部目录中存储图像时出现 IllegalArgumentException 错误
IllegalArgumentException error on storing images in external directory
我想点击一张图片存入外存目录。为此,我正在使用这个:
public class MainActivity extends AppCompatActivity {
Button btn_pic;
ImageView iv_pic;
Context ctx = this;
Bitmap photo, bm;
String photoPath, img_dp;
private static final int CAMERA_REQUEST = 1888;
private int STORAGE_PERMISSION_CODE = 23;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_pic = (Button)findViewById(R.id.btn_pic);
iv_pic = (ImageView)findViewById(R.id.iv_pic);
btn_pic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
if(intent.resolveActivity(getPackageManager()) != null){
File photoFile = null;
try{
photoFile = createImageFile();
}
catch (Exception e){
e.printStackTrace();
}
if(photoFile != null){
Uri photoUri = FileProvider.getUriForFile(ctx, "com.example.android.fileprovider", photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, CAMERA_REQUEST);
}
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
photo = (Bitmap) data.getExtras().get("data");
bm = BitmapFactory.decodeFile(photoPath);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] b = byteArrayOutputStream.toByteArray();
img_dp = Base64.encodeToString(b, Base64.DEFAULT);
Log.i("image convert", img_dp);
iv_pic.setImageBitmap(photo);
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("ddmmyyyy_HHmmss").format(new Date());
String imgFileName = "JPEG_" + timeStamp;
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File img = File.createTempFile(imgFileName, ".jpg", storageDir);
photoPath = img.getAbsolutePath();
return img;
}
}
我的 paths.xml 文件是:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.falcon.apps.imagestoragedemo/files/Pictures" />
</paths>
我已经在我的 AndroidManifest.xml 文件中声明了一个提供商
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths"></meta-data>
</provider>
我收到以下错误:
The error I am getting
为什么会出现此错误以及如何解决?
除了这一行,你的代码看起来还不错:
Uri photoUri = FileProvider.getUriForFile(ctx, "com.example.android.fileprovider", photoFile);
不确定您为什么 com.example.android.fileprovider
或者您是否只是从一些在线资源复制而来。
检查此 Android Documentation for Saving Files。它有您需要的确切代码。
编辑:Link
Edit2:还要检查你的清单以防 IllegalArgumentException 没有解决。
所以,经过几个小时的搜索和尝试,我终于解决了这个问题。因为,我没有指定特定路径,只是调用 getExternalStoragePublicDirectory()
,所以我不需要在 paths.xml 文件中指定路径。
修改后的paths.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"/>
</paths>
这里external-path标签相当于调用getExternalStoragePublicDirectory()/getExternalStorageDirectory()
.
我想点击一张图片存入外存目录。为此,我正在使用这个:
public class MainActivity extends AppCompatActivity {
Button btn_pic;
ImageView iv_pic;
Context ctx = this;
Bitmap photo, bm;
String photoPath, img_dp;
private static final int CAMERA_REQUEST = 1888;
private int STORAGE_PERMISSION_CODE = 23;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_pic = (Button)findViewById(R.id.btn_pic);
iv_pic = (ImageView)findViewById(R.id.iv_pic);
btn_pic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
if(intent.resolveActivity(getPackageManager()) != null){
File photoFile = null;
try{
photoFile = createImageFile();
}
catch (Exception e){
e.printStackTrace();
}
if(photoFile != null){
Uri photoUri = FileProvider.getUriForFile(ctx, "com.example.android.fileprovider", photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, CAMERA_REQUEST);
}
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
photo = (Bitmap) data.getExtras().get("data");
bm = BitmapFactory.decodeFile(photoPath);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] b = byteArrayOutputStream.toByteArray();
img_dp = Base64.encodeToString(b, Base64.DEFAULT);
Log.i("image convert", img_dp);
iv_pic.setImageBitmap(photo);
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("ddmmyyyy_HHmmss").format(new Date());
String imgFileName = "JPEG_" + timeStamp;
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File img = File.createTempFile(imgFileName, ".jpg", storageDir);
photoPath = img.getAbsolutePath();
return img;
}
}
我的 paths.xml 文件是:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.falcon.apps.imagestoragedemo/files/Pictures" />
</paths>
我已经在我的 AndroidManifest.xml 文件中声明了一个提供商
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths"></meta-data>
</provider>
我收到以下错误:
The error I am getting
为什么会出现此错误以及如何解决?
除了这一行,你的代码看起来还不错:
Uri photoUri = FileProvider.getUriForFile(ctx, "com.example.android.fileprovider", photoFile);
不确定您为什么 com.example.android.fileprovider
或者您是否只是从一些在线资源复制而来。
检查此 Android Documentation for Saving Files。它有您需要的确切代码。
编辑:Link
Edit2:还要检查你的清单以防 IllegalArgumentException 没有解决。
所以,经过几个小时的搜索和尝试,我终于解决了这个问题。因为,我没有指定特定路径,只是调用 getExternalStoragePublicDirectory()
,所以我不需要在 paths.xml 文件中指定路径。
修改后的paths.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"/>
</paths>
这里external-path标签相当于调用getExternalStoragePublicDirectory()/getExternalStorageDirectory()
.