我正在使用相机捕获图像并将返回的位图设置为imageview,但它在OnePlus设备上崩溃。我检查了几台htc设备,它运行正常。我检查了解决方案,他们说检查请求代码,但我已经在做了。这可能是什么问题?
这是代码
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1);
}
});
和onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == -1) {
image = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Must compress the Image to reduce image size to make upload easy
image.compress(Bitmap.CompressFormat.PNG, 50, stream);
byte[] byte_arr = stream.toByteArray();
// Encode Image to String
encodedString = Base64.encodeToString(byte_arr, 0);
Uri selectedImageUri = data.getData();
img_path = getRealPathFromURI(selectedImageUri);
// Uri tempUri = getImageUri(getApplicationContext(), image);
imageView.setImageBitmap(image);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
和错误的堆栈跟踪
STACK_TRACE = java.lang.RuntimeException:传递结果失败 ResultInfo {who = null,request = 1,result = -1,data = Intent { act = inline-data(has extras)}} to activity {com.madhours / com.madhours.activities.ActivitySignUp}: java.lang.NullPointerException:尝试调用虚方法 空对象上的'java.lang.String android.net.Uri.getPath()' 参考 android.app.ActivityThread.deliverResults(ActivityThread.java:3659) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3702) 在android.app.ActivityThread.access $ 1300(ActivityThread.java:155) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1366) 在android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:135)at android.app.ActivityThread.main(ActivityThread.java:5343)at java.lang.reflect.Method.invoke(Native Method)at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 引起:java.lang.NullPointerException:尝试调用虚拟 null对象上的方法'java.lang.String android.net.Uri.getPath()' 参考 com.madhours.activities.ActivitySignUp.getRealPathFromURI(ActivitySignUp.java:134) 在 com.madhours.activities.ActivitySignUp.onActivityResult(ActivitySignUp.java:116) 在android.app.Activity.dispatchActivityResult(Activity.java:6218) 在 android.app.ActivityThread.deliverResults(ActivityThread.java:3655) ... 10更多java.lang.NullPointerException:尝试调用虚拟 null对象上的方法'java.lang.String android.net.Uri.getPath()' 参考 com.madhours.activities.ActivitySignUp.getRealPathFromURI(ActivitySignUp.java:134) 在 com.madhours.activities.ActivitySignUp.onActivityResult(ActivitySignUp.java:116) 在android.app.Activity.dispatchActivityResult(Activity.java:6218) 在 android.app.ActivityThread.deliverResults(ActivityThread.java:3655) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3702) 在android.app.ActivityThread.access $ 1300(ActivityThread.java:155) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1366) 在android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:135)at android.app.ActivityThread.main(ActivityThread.java:5343)at java.lang.reflect.Method.invoke(Native Method)at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
答案 0 :(得分:1)
这里有什么问题?
问题是您在返回的getData()
上呼叫Intent
。返回Uri
不属于the ACTION_MEDIA_STORE
contract:
调用者可以传递额外的EXTRA_OUTPUT来控制该图像的写入位置。如果EXTRA_OUTPUT不存在,则在额外字段中返回小尺寸图像作为Bitmap对象。这对于只需要小图像的应用程序非常有用。如果存在EXTRA_OUTPUT,则将将全尺寸图像写入EXTRA_OUTPUT的Uri值。
鉴于您没有将EXTRA_OUTPUT
放在Intent
startActivityForResult()
上,Bitmap
只会data
EXTRA_OUTPUT
onActivityResult()
额外的费用。
如果您想要在某处写入完整尺寸的图片,请在/***
Copyright (c) 2008-2016 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.camcon;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;
public class CameraContentDemoActivity extends Activity {
private static final String EXTRA_FILENAME=
"com.commonsware.android.camcon.EXTRA_FILENAME";
private static final String FILENAME="CameraContentDemo.jpeg";
private static final int CONTENT_REQUEST=1337;
private File output=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (savedInstanceState==null) {
File dir=
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
dir.mkdirs();
output=new File(dir, FILENAME);
}
else {
output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
}
if (output.exists()) {
output.delete();
}
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(i, CONTENT_REQUEST);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(EXTRA_FILENAME, output);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == CONTENT_REQUEST) {
if (resultCode == RESULT_OK) {
Intent i=new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(output), "image/jpeg");
startActivity(i);
finish();
}
}
}
}
中提供指向该位置的路径,然后按住该路径并在调用{{1}}时使用该路径:
{{1}}