Android 应用程序崩溃,因为URI提供NULLPOINTEREXCEPTION
我正在开发一个应用程序,通过gallery和camera将图像上传到我的服务器。我的问题是,我的一些代码中出现了NullPointerException,我不知道如何修复它。这个错误发生在我的活动中,我打电话给摄像机并上传了其他问题的图像,所以没有帮助我 根据日志,是这条线Android 应用程序崩溃,因为URI提供NULLPOINTEREXCEPTION,android,nullpointerexception,android-camera,uri,Android,Nullpointerexception,Android Camera,Uri,我正在开发一个应用程序,通过gallery和camera将图像上传到我的服务器。我的问题是,我的一些代码中出现了NullPointerException,我不知道如何修复它。这个错误发生在我的活动中,我打电话给摄像机并上传了其他问题的图像,所以没有帮助我 根据日志,是这条线 cursor.moveToFirst(); 以及 photo = (Bitmap) data.getExtras().get("data"); 日志也给出了错误 java.lang.RuntimeException: F
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>
fest xmlns:android=”http://schemas.android.com/apk/res/android"
xmlns:tools=”http://schemas.android.com/tools"
package=“com.smartpractice.myapplication”>
单击按钮后,它必须将我发送到活动,从那里它将打开摄像头删除所有
光标
内容。不应该有ACTION\u IMAGE\u CAPTURE
返回的Uri
。而且,即使存在Uri
,也不需要MediaStore
知道它,而且数据列在Android Q和更高版本上不再可用
您的图像将是来自data.getExtras().get(“数据”)的缩略图位图代码>
或者,在EXTRA_OUTPUT
中提供您自己的Uri
(而不是您现在提供的null
),这样您就可以控制图像的存储位置。显示如何将FileProvider
与EXTRA_输出一起用于此目的。可能重复的可能重复解决了它崩溃和空指针的问题,但现在在显示thumbnaail并单击“上载”后,它崩溃了completely@RubenMeiring:使用Logcat检查崩溃的堆栈跟踪。如果无法解释崩溃的原因,请使用当前代码和完整的堆栈跟踪单独询问堆栈溢出问题。