应用程序崩溃,因为 URI 给出 NULLPOINTEREXCEPTION [英] App Crashing because URI give NULLPOINTEREXCEPTION
问题描述
正在开发一个应用程序,通过图库和相机将图像上传到我的服务器 我的问题是我的某些代码出现 NullPointerException,但我不知道如何解决这个问题.错误发生在我调用相机并上传图像的活动中 关于 SO 的其他问题对我没有帮助
am developing an app to upload images to my server via gallery and camera My problem is I get a NullPointerException on some of my code and I have NO idea of how to fix this. the error occurs in my activity that calls the camera and uploads the image Other Questions on SO didn't help me
根据日志,是这一行
cursor.moveToFirst();
以及
photo = (Bitmap) data.getExtras().get("data");
日志也给出了错误
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.smartpractice.myapplication/com.smartpractice.myapplication.CameraActivity}: java.lang.NullPointerException: uri
现在附上代码
相机活动
public class CameraActivity extends Activity {
Button btpic, btnup;
private Uri fileUri;
String picturePath;
Uri selectedImage;
Bitmap photo;
String ba1;
public static String URL = "https://www.smartpractice.co.za/files-upload-ruben.asp?MyForm=Yes";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
btpic = (Button) findViewById(R.id.cpic);
btpic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clickpic();
}
});
btnup = (Button) findViewById(R.id.up);
btnup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
upload();
}
});
}
private void upload() {
// Image location URL
Log.e("path", "----------------" + picturePath);
// Image
Bitmap bm = BitmapFactory.decodeFile(picturePath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] ba = bao.toByteArray();
ba1 = Base64.encodeToString(ba, Base64.NO_WRAP);
Log.e("base64", "-----" + ba1);
// Upload image to server
new uploadToServer().execute();
}
private void clickpic() {
// Check Camera
if (getApplicationContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// Open default camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, 100);
} else {
Toast.makeText(getApplication(), "Camera not supported", Toast.LENGTH_LONG).show();
}
}
此处出现错误
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100 && resultCode == RESULT_OK) {
selectedImage = data.getData();
**photo = (Bitmap) data.getExtras().get("data");**
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
**cursor.moveToFirst();**
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView =findViewById(R.id.Imageprev);
imageView.setImageBitmap(photo);
}
}
主要活动
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonOne=findViewById(R.id.activity2btn);
buttonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent activity2Intent=new Intent(getApplicationContext(), CameraActivity.class);
startActivity(activity2Intent);
}
});
Button buttonTwo=findViewById(R.id.activity2btn2);
buttonTwo.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent activity2Intent=new Intent(getApplicationContext(), UploadActivity.class);
startActivity(activity2Intent);
}
});
}
}
清单
fest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.smartpractice.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>
<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"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".TimeLogActivity"/>
<activity android:name=".CameraActivity"
/>
<activity android:name=".UploadActivity" >
<intent-filter>
<action android:name="andriod.intent.action.main"/>
</intent-filter>
</activity>
<activity android:name=".LoginActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
点击按钮后,它必须将我发送到活动,从那里打开相机
After the button click, it must send me to the activity from there it opens the camera
推荐答案
删除所有 Cursor
内容.ACTION_IMAGE_CAPTURE
不应该返回 Uri
.而且,即使有 Uri
,也不需要 MediaStore
知道它,并且 DATA
列不再可用在 Android Q 及更高版本上.
Delete all the Cursor
stuff. There is not supposed to be a Uri
returned by ACTION_IMAGE_CAPTURE
. And, even if there were a Uri
, there is no requirement for the MediaStore
to know about it, and the DATA
column is no longer available on Android Q and higher.
您的图像将是 data.getExtras().get("data");
中的缩略图 Bitmap
.
Your image will be the thumbnail Bitmap
from data.getExtras().get("data");
.
或者,在 EXTRA_OUTPUT
中提供您自己的 Uri
(而不是您现在提供的 null
),以便您控制图像的位置应该存储.这个示例应用展示了如何使用FileProvider
与 EXTRA_OUTPUT
用于此目的.
Alternatively, provide your own Uri
in EXTRA_OUTPUT
(instead of the null
that you are providing now), so you control where the image should be stored. This sample app shows how to use FileProvider
with EXTRA_OUTPUT
for this purpose.
这篇关于应用程序崩溃,因为 URI 给出 NULLPOINTEREXCEPTION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!