应用程序崩溃,因为 URI 给出 NULLPOINTEREXCEPTION
App Crashing because URI give NULLPOINTEREXCEPTION
正在开发一个应用程序以通过图库和相机将图像上传到我的服务器我的问题是我的一些代码出现了 NullPointerException,我不知道如何解决这个问题。我的 activity 调用相机并上传图像时出现错误 其他关于 SO 的问题对我没有帮助
根据日志,是这一行
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
现在附上代码
相机Activity
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);
}
}
主要Activity
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>
点击按钮后,它必须将我发送到 activity 从那里它会打开相机
删除所有 Cursor
内容。 ACTION_IMAGE_CAPTURE
不应返回 Uri
。而且,即使有 Uri
,也不需要 MediaStore
知道它,DATA
列在 Android Q 及更高版本上不再可用.
您的图片将是 data.getExtras().get("data");
的缩略图 Bitmap
。
或者,在 EXTRA_OUTPUT
中提供您自己的 Uri
(而不是您现在提供的 null
),这样您就可以控制图像的存储位置。 This sample app 展示了如何将 FileProvider
与 EXTRA_OUTPUT
一起用于此目的。
正在开发一个应用程序以通过图库和相机将图像上传到我的服务器我的问题是我的一些代码出现了 NullPointerException,我不知道如何解决这个问题。我的 activity 调用相机并上传图像时出现错误 其他关于 SO 的问题对我没有帮助
根据日志,是这一行
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
现在附上代码
相机Activity
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);
}
}
主要Activity
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>
点击按钮后,它必须将我发送到 activity 从那里它会打开相机
删除所有 Cursor
内容。 ACTION_IMAGE_CAPTURE
不应返回 Uri
。而且,即使有 Uri
,也不需要 MediaStore
知道它,DATA
列在 Android Q 及更高版本上不再可用.
您的图片将是 data.getExtras().get("data");
的缩略图 Bitmap
。
或者,在 EXTRA_OUTPUT
中提供您自己的 Uri
(而不是您现在提供的 null
),这样您就可以控制图像的存储位置。 This sample app 展示了如何将 FileProvider
与 EXTRA_OUTPUT
一起用于此目的。