gpt4 book ai didi

java - 为什么返回空数据是否保存在内部存储中,因为没有 sd 卡手机无法工作

转载 作者:行者123 更新时间:2023-11-29 02:31:07 26 4
gpt4 key购买 nike

为什么返回空数据返回是否保存在内部存储中,因为当我在捕获后返回时在没有 sd 卡手机的情况下无法工作然后在 redmii 手机中崩溃应用程序,这是 logcat

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null}

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
at com.logiclump.technologies.gigmedico.Home.onActivityResult(Home.java:130)
at android.app.Activity.dispatchActivityResult(Activity.java:6562)

这是第一个 Activity

Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = Uri.fromFile(getOutputMediaFile());
i.putExtra(MediaStore.EXTRA_OUTPUT, file);

startActivityForResult(i, CAMERA_REQUEST_CODE);


private static File getOutputMediaFile()
{
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "FotoAula");

if (!mediaStorageDir.exists()){
if (!mediaStorageDir.mkdirs()){
return null;
}
}

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == CAMERA_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
file.getLastPathSegment();
file=data.getData();
if (file !=null)
{
Intent intent = new Intent(this, PictureActivity.class);
intent.putExtra("imgUrl", file.toString());
startActivity(intent);
}
}
}

这是我在 imageView 中设置图像的第二个 Activity

  Bundle bundle = getIntent().getExtras();

if (bundle != null) {
Log.e("ashish", bundle.getString("imgUrl") + "");
path = Uri.parse(bundle.getString("imgUrl"));

}

ImageView selfiiii = (ImageView) findViewById(R.id.mySelfie);
selfiiii.setImageURI(path);

最佳答案

更改 onActvityResult 代码。

 Instead

file.getLastPathSegment();
file=data.getData();
if (file !=null)
{
Intent intent = new Intent(this, PictureActivity.class);
intent.putExtra("imgUrl", file.toString());
startActivity(intent);
}
**try this**
File file = getOutputMediaFile();
String path = getPath(Uri.fromFile(getOutputMediaFile()));
Intent intent = new Intent(this, PictureActivity.class);
intent.putExtra("imgUrl", path );
startActivity(intent);

第二个 Activity

Bundle bundle = getIntent().getExtras();

uriPath = bundle.getString("imgUrl");

getPath 方法

**

private String getPath(Uri uri) throws URISyntaxException {
final boolean needToCheckUri = Build.VERSION.SDK_INT >= 19;
String selection = null;
String[] selectionArgs = null;
// Uri is different in versions after KITKAT (Android 4.4), we need to
// deal with different Uris.
if (needToCheckUri && DocumentsContract.isDocumentUri(getApplicationContext(), uri)) {
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
return Environment.getExternalStorageDirectory() + "/" + split[1];
} else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
uri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
} else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("image".equals(type)) {
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
selection = "_id=?";
selectionArgs = new String[]{
split[1]
};
}
}
if ("content".equalsIgnoreCase(uri.getScheme())) {
String[] projection = {
MediaStore.Images.Media.DATA
};
Cursor cursor = null;
try {
cursor = getContentResolver()
.query(uri, projection, selection, selectionArgs, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if (cursor.moveToFirst()) {
return cursor.getString(column_index);
}
} catch (Exception e) {
e.printStackTrace();
}
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}**



/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}

/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}

/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}

关于java - 为什么返回空数据是否保存在内部存储中,因为没有 sd 卡手机无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49553906/

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