Java Android 4.4.2中的Uri.parse(path)返回null
我有一个应用程序,允许用户上传墙上的照片 这段代码对大多数用户来说都很好,但我曾经报告过,在上传照片时,应用程序有时会崩溃 问题不在于从相机拍摄照片,而在于你必须拍摄照片的路径 导致这个问题的安卓版本是4.4.2,但我不知道如何修复它 发布一些代码: 活动结果:Java Android 4.4.2中的Uri.parse(path)返回null,java,android,nullpointerexception,uri,Java,Android,Nullpointerexception,Uri,我有一个应用程序,允许用户上传墙上的照片 这段代码对大多数用户来说都很好,但我曾经报告过,在上传照片时,应用程序有时会崩溃 问题不在于从相机拍摄照片,而在于你必须拍摄照片的路径 导致这个问题的安卓版本是4.4.2,但我不知道如何修复它 发布一些代码: 活动结果: protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) {
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_PIC_REQUEST) {
try {
//picUri is a global variable Uri
picUri = data.getData();
cropImage();
} catch (Exception e) {
e.printStackTrace();
}
}
else if(requestCode == PIC_CROP) {
try{
//thumbnail is a global variable Bitmap
thumbnail = MediaStore.Images.Media.getBitmap(context.getContentResolver(), cropImageUri);
setImage();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
热裁剪图像:
public void cropImage() {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("scale", true);
//indicate output X and Y
cropIntent.putExtra("outputX", 700);
cropIntent.putExtra("outputY", 700);
//retrieve data on return
cropIntent.putExtra("return-data", false);
File f = createNewFile("CROP_");
try{
f.createNewFile();
}
catch (IOException e) {
e.printStackTrace();
}
//cropImageUri is a global variable Uri
cropImageUri = Uri.fromFile(f);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, cropImageUri);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
catch(ActivityNotFoundException anfe){
anfe.printStackTrace();
}
}
private File createNewFile(String prefix) {
if (prefix== null) {
prefix="IMG_";
}
else if("".equalsIgnoreCase(prefix)) {
prefix="IMG_";
}
File newDirectory = new File(Environment.getExternalStorageDirectory()+"/mypics/");
if (!newDirectory.exists()) {
if (newDirectory.mkdir()) {
}
}
File file = new File(newDirectory,(prefix+System.currentTimeMillis()+".jpg"));
if (file.exists()) {
file.delete();
try {
file.createNewFile();
}catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
public void preUploadImage() {
UploadImage uploadImage = new UploadImage();
Uri newUri = getImageUri(thumbnail);
try{
// System.out.println("uri = "+picUri);
uploadImage.upload(getRealPathFromURI(newUri));
}
catch (IOException e) {
e.printStackTrace();
}
}
public Uri getImageUri(Bitmap inImage) {
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
创建新文件:
public void cropImage() {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("scale", true);
//indicate output X and Y
cropIntent.putExtra("outputX", 700);
cropIntent.putExtra("outputY", 700);
//retrieve data on return
cropIntent.putExtra("return-data", false);
File f = createNewFile("CROP_");
try{
f.createNewFile();
}
catch (IOException e) {
e.printStackTrace();
}
//cropImageUri is a global variable Uri
cropImageUri = Uri.fromFile(f);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, cropImageUri);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
catch(ActivityNotFoundException anfe){
anfe.printStackTrace();
}
}
private File createNewFile(String prefix) {
if (prefix== null) {
prefix="IMG_";
}
else if("".equalsIgnoreCase(prefix)) {
prefix="IMG_";
}
File newDirectory = new File(Environment.getExternalStorageDirectory()+"/mypics/");
if (!newDirectory.exists()) {
if (newDirectory.mkdir()) {
}
}
File file = new File(newDirectory,(prefix+System.currentTimeMillis()+".jpg"));
if (file.exists()) {
file.delete();
try {
file.createNewFile();
}catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
public void preUploadImage() {
UploadImage uploadImage = new UploadImage();
Uri newUri = getImageUri(thumbnail);
try{
// System.out.println("uri = "+picUri);
uploadImage.upload(getRealPathFromURI(newUri));
}
catch (IOException e) {
e.printStackTrace();
}
}
public Uri getImageUri(Bitmap inImage) {
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
然后,当用户单击“发送”方法时,会调用preUploadImage:
public void cropImage() {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("scale", true);
//indicate output X and Y
cropIntent.putExtra("outputX", 700);
cropIntent.putExtra("outputY", 700);
//retrieve data on return
cropIntent.putExtra("return-data", false);
File f = createNewFile("CROP_");
try{
f.createNewFile();
}
catch (IOException e) {
e.printStackTrace();
}
//cropImageUri is a global variable Uri
cropImageUri = Uri.fromFile(f);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, cropImageUri);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
catch(ActivityNotFoundException anfe){
anfe.printStackTrace();
}
}
private File createNewFile(String prefix) {
if (prefix== null) {
prefix="IMG_";
}
else if("".equalsIgnoreCase(prefix)) {
prefix="IMG_";
}
File newDirectory = new File(Environment.getExternalStorageDirectory()+"/mypics/");
if (!newDirectory.exists()) {
if (newDirectory.mkdir()) {
}
}
File file = new File(newDirectory,(prefix+System.currentTimeMillis()+".jpg"));
if (file.exists()) {
file.delete();
try {
file.createNewFile();
}catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
public void preUploadImage() {
UploadImage uploadImage = new UploadImage();
Uri newUri = getImageUri(thumbnail);
try{
// System.out.println("uri = "+picUri);
uploadImage.upload(getRealPathFromURI(newUri));
}
catch (IOException e) {
e.printStackTrace();
}
}
public Uri getImageUri(Bitmap inImage) {
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
最后一行出现错误。
return Uri.parse(path);
此行导致出现NullPointerException
java.lang.NullPointerException: uriString
at android.net.Uri$StringUri.<init>(Uri.java:468)
at android.net.Uri$StringUri.<init>(Uri.java:458)
at android.net.Uri.parse(Uri.java:430)
at com.delsorboilario.verdebio.ScriviDomanda.getImageUri(ScriviDomanda.java:584)
at com.delsorboilario.verdebio.ScriviDomanda.preUploadImage(ScriviDomanda.java:608)
at com.delsorboilario.verdebio.ScriviDomanda$6$4$2.run(ScriviDomanda.java:292)
java.lang.NullPointerException:uriString
在android.net.Uri$StringUri。(Uri.java:468)
在android.net.Uri$StringUri。(Uri.java:458)
位于android.net.Uri.parse(Uri.java:430)
在com.delsorboliario.verdebio.ScriviDomanda.getImageUri上(ScriviDomanda.java:584)
在com.delsorboliario.verdebio.ScriviDomanda.preUploadImage(ScriviDomanda.java:608)上
在com.delsorboliario.verdebio.ScriviDomanda$6$4$2.run(ScriviDomanda.java:292)
看起来像MediaStore.Images.Media.insertImage(context.getContentResolver(),在图像中,“Title”,null)代码>返回空值
形成
插入图像并为其创建缩略图
参数
cr要使用的内容解析器
源用于图像的流
标题图像的名称
描述图像的描述
返回新创建的图像的URL,如果图像为空,则返回null
由于任何原因无法存储
看起来像MediaStore.Images.Media.insertImage(context.getContentResolver(),inImage,“Title”,null)代码>返回空值
形成
插入图像并为其创建缩略图
参数
cr要使用的内容解析器
源用于图像的流
标题图像的名称
描述图像的描述
返回新创建的图像的URL,如果图像为空,则返回null
由于任何原因无法存储
我没有尝试使用insertImage()
,在本例中,不太清楚您为什么需要它
首先,看起来你是想上传照片。为此,您只需要在createNewFile()
中创建的文件
。如果您自己上传它(例如,HttpUrlConnection
,某个第三方库),您应该能够在上面使用文件
或输入流
。即使上传代码确实需要一个Uri
,您也可以尝试Uri.fromFile()
从您的文件中获取Uri
,看看是否有效
MediaStore
发挥作用的地方是将您的文件编入索引,从而使应用程序(查询MediaStore
图像的应用程序)和用户(通过其USB电缆的MTP连接)能够访问您的文件MediaScannerConnection
及其静态scanFile()
方法是一种相当简单的文件索引方法。只需确保首先将文件完全写入磁盘(例如,如果您自己正在写入文件,请在文件输出流上调用getFD().sync()
在刷新()
之后和关闭()
)之前。我没有尝试使用插入图像()
,在这种情况下,不太清楚你为什么需要它
首先,看起来你是想上传照片。为此,您只需要在createNewFile()
中创建的文件
。如果您自己上传它(例如,HttpUrlConnection
,某个第三方库),您应该能够在上面使用文件
或输入流
。即使上传代码确实需要一个Uri
,您也可以尝试Uri.fromFile()
从您的文件中获取Uri
,看看是否有效
MediaStore
发挥作用的地方是将您的文件编入索引,从而使应用程序(查询MediaStore
图像的应用程序)和用户(通过其USB电缆的MTP连接)能够访问您的文件MediaScannerConnection
及其静态scanFile()
方法是一种相当简单的文件索引方法。只需确保首先将文件完全写入磁盘(例如,如果您自己正在写入文件,请在FileOutputStream
上调用getFD()??如何修复它。。。那么问题是存储图像吗?@Ilario是的问题是存储图像。@Ilario看,第一个答案很没用。。。最后一个看起来很有希望,但是我如何创建上面提到的目录?使用文件对象?!是的,但为什么只有在某些版本的Android中??如何修复它。。。那么问题是存储图像吗?@Ilario是的问题是存储图像。@Ilario看,第一个答案很没用。。。最后一个看起来很有希望,但是我如何创建上面提到的目录?使用文件对象?!首先。其次,@commonware噢,谢谢。。我在这方面从来没有遇到过问题。。我将尝试使用本文中的库。现在你有什么建议来解决我的问题吗?感谢您,我从未尝试使用insertImage()
。如果我想将文件写入外部存储器,我会将文件写入外部存储器,然后使用MediaScannerConnection
和scanFile()
获取由MediaStore
索引的图像@