问题描述
我在做一个摄像头应用程序,使用code我在互联网上发现,捕捉和农作物的图像。直到我想将图像保存在SDD卡上,做精了。它意外停止,并产生这些错误:
15 08-06:06:58.341:E / AndroidRuntime(26462):致命异常:主要08-06 15:06:58.341:E / AndroidRuntime(26462):了java.lang.RuntimeException:不提供结果ResultInfo {谁= NULL,请求= 2,结果= -1,数据= {意向(有临时演员)}}到活动{} net.londatiga.android/net.londatiga.android.MainActivity:显示java.lang.NullPointerException08-06 15:06:58.341:E / AndroidRuntime(26462):在android.app.ActivityThread.deliverResults(ActivityThread.java:2994)08-06 15:06:58.341:E / AndroidRuntime(26462):在android.app.ActivityThread.handleSendResult(ActivityThread.java:3037)08-06 15:06:58.341:E / AndroidRuntime(26462):在android.app.ActivityThread.access $ 1100(ActivityThread.java:128)08-06 15:06:58.341:E / AndroidRuntime(26462):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1191)08-06 15:06:58.341:E / AndroidRuntime(26462):在android.os.Handler.dispatchMessage(Handler.java:99)08-06 15:06:58.341:E / AndroidRuntime(26462):在android.os.Looper.loop(Looper.java:137)08-06 15:06:58.341:E / AndroidRuntime(26462):在android.app.ActivityThread.main(ActivityThread.java:4514)08-06 15:06:58.341:E / AndroidRuntime(26462):在java.lang.reflect.Method.invokeNative(本机方法)08-06 15:06:58.341:E / AndroidRuntime(26462):在java.lang.reflect.Method.invoke(Method.java:511)08-06 15:06:58.341:E / AndroidRuntime(26462):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:993)08-06 15:06:58.341:E / AndroidRuntime(26462):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)08-06 15:06:58.341:E / AndroidRuntime(26462):在dalvik.system.NativeStart.main(本机方法)08-06 15:06:58.341:E / AndroidRuntime(26462):致:显示java.lang.NullPointerException08-06 15:06:58.341:E / AndroidRuntime(26462):在java.io.File.fixSlashes(File.java:185)。08-06 15:06:58.341:E / AndroidRuntime(26462):在java.io.File中的<&初始化GT;(File.java:134)08-06 15:06:58.341:E / AndroidRuntime(26462):在net.londatiga.android.MainActivity.saveBitmapToFile(MainActivity.java:128)08-06 15:06:58.341:E / AndroidRuntime(26462):在net.londatiga.android.MainActivity.onActivityResult(MainActivity.java:112)08-06 15:06:58.341:E / AndroidRuntime(26462):在android.app.Activity.dispatchActivityResult(Activity.java:4746)08-06 15:06:58.341:E / AndroidRuntime(26462):在android.app.ActivityThread.deliverResults(ActivityThread.java:2990)08-06 15:06:58.341:E / AndroidRuntime(26462):... 11个
总code:
包net.londatiga.android;进口的java.io.File;
进口java.io.FileOutputStream中;
进口的java.util.ArrayList;
进口的java.util.List;进口android.app.Activity;
进口android.app.AlertDialog;
进口android.content.ActivityNotFoundException;
进口android.content.ComponentName;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.content.pm.ResolveInfo;
进口android.graphics.Bitmap;
进口android.graphics.Bitmap.Com pressFormat;
进口android.graphics.Path;
进口android.net.Uri;
进口android.os.Bundle;
进口android.os.Environment;
进口android.provider.MediaStore;
进口android.view.View;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.ImageView;
进口android.widget.Toast;公共类MainActivity延伸活动{
私人乌里mImageCaptureUri;
私人ImageView的mImageView;
私人字符串路径;
私人位图位图; 私有静态最终诠释PICK_FROM_CAMERA = 1;
私有静态最终诠释CROP_FROM_CAMERA = 2;
私有静态最终诠释PICK_FROM_FILE = 3;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 的setContentView(R.layout.main); 最终的String []项目=新的String [] {从相机拍摄,从库中选择};
ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT; (这一点,android.R.layout.select_dialog_item,项目);
AlertDialog.Builder建设者=新AlertDialog.Builder(本); builder.setTitle(选择图片);
builder.setAdapter(适配器,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释项){//从镜头
如果(项目== 0){
意向意图=新意图(MediaStore.ACTION_IMAGE_CAPTURE); mImageCaptureUri = Uri.fromFile(新文件(Environment.getExternalStorageDirectory()
tmp_avatar_+将String.valueOf(System.currentTimeMillis的())+.JPG)); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,mImageCaptureUri); 尝试{
intent.putExtra(回归数据,真正的); startActivityForResult(意向,PICK_FROM_CAMERA);
}赶上(ActivityNotFoundException E){
e.printStackTrace();
}
}其他{//从文件中挑选
意向意图=新的Intent(); intent.setType(图像/ *);
intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(原意,使用完整的行动),PICK_FROM_FILE);
}
}
}); 最后AlertDialog对话框= builder.create(); Button按钮=(按钮)findViewById(R.id.btn_crop);
mImageView =(ImageView的)findViewById(R.id.iv_photo); button.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
dialog.show();
}
});
} @覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
如果(结果code = RESULT_OK!)回报; 开关(要求code){
案例PICK_FROM_CAMERA:
doCrop(); 打破; 案例PICK_FROM_FILE:
mImageCaptureUri = data.getData(); doCrop(); 打破; 案例CROP_FROM_CAMERA:
捆绑额外= data.getExtras(); 如果(临时演员!= NULL){
位图照片= extras.getParcelable(数据);
saveBitmapToFile(/ SD卡/ cropped_img.jpg,照片); mImageView.setImageBitmap(照片);
} 文件f =新的文件(mImageCaptureUri.getPath()); 如果(f.exists())f.delete(); 打破;
//
}
} 私人布尔saveBitmapToFile(串串,位图图片){ 档案文件=新的文件(路径);
布尔解析度= FALSE;如果(!file.exists())
{
尝试
{
FOS的FileOutputStream =新的FileOutputStream(文件);
RES = bitmap.com preSS(比较pressFormat.JPEG,100,FOS); fos.close();
}赶上(例外五)
{}
}返回水库;
}//} 私人无效doCrop(){
最终的ArrayList< CropOption> cropOptions =新的ArrayList< CropOption>(); 意向意图=新意图(com.android.camera.action.CROP);
intent.setType(图像/ *); 清单< ResolveInfo> 。名单= getPackageManager()queryIntentActivities(意向,0); INT大小=则为list.size(); 如果(大小== 0){
Toast.makeText(这一点,找不到图像裁剪的应用程序,Toast.LENGTH_SHORT).show(); 返回;
}其他{
intent.setData(mImageCaptureUri); intent.putExtra(outputX,200);
intent.putExtra(outputY,200);
intent.putExtra(aspectX,1);
intent.putExtra(aspectY,1);
intent.putExtra(规模化,真正的);
intent.putExtra(回归数据,真正的); 如果(大小== 1){
意图I =新意图(意向);
ResolveInfo解析度= list.get(0); i.setComponent(新组件名(res.activityInfo.packageName,res.activityInfo.name)); startActivityForResult(ⅰ,CROP_FROM_CAMERA);
}其他{
对于(ResolveInfo RES:名单){
最后CropOption共同=新CropOption(); co.title = getPackageManager()getApplicationLabel(res.activityInfo.applicationInfo)。
co.icon = getPackageManager()getApplicationIcon(res.activityInfo.applicationInfo)。
co.appIntent =新意图(意向); co.appIntent.setComponent(新组件名(res.activityInfo.packageName,res.activityInfo.name)); cropOptions.add(共);
} CropOptionAdapter适配器=新CropOptionAdapter(getApplicationContext(),cropOptions); AlertDialog.Builder建设者=新AlertDialog.Builder(本);
builder.setTitle(选择裁剪应用程序);
builder.setAdapter(适配器,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释项){
startActivityForResult(cropOptions.get(项目).appIntent,CROP_FROM_CAMERA);
}
}); builder.setOnCancelListener(新DialogInterface.OnCancelListener(){
公共无效onCancel(DialogInterface对话){ 如果(mImageCaptureUri!= NULL){
。getContentResolver()删除(mImageCaptureUri,NULL,NULL);
mImageCaptureUri = NULL;
}
}
}); AlertDialog警报= builder.create(); alert.show();
}
}
}
}
您也越来越NPE。看到这一行bitmap.com preSS(比较pressFormat.JPEG,100,FOS); fos.close(); 。被分配了一个y值的位图的变量。我没有看到它在你的code。 instread尝试photo..com preSS(比较pressFormat.JPEG,100,FOS);
有在code许多错误。
上面提到的就是其中之一。
我刚才看到你所得到的文件的文件=新的文件(路径)异常;
没有定义这个变量路径。与你的路径值初始化。
I'm doing a camera application, using the code I found on the internet, that captures and crops images. It is doing fine up until I want to save the image in the SDd card. It stops unexpectedly and produces these errors:
08-06 15:06:58.341: E/AndroidRuntime(26462): FATAL EXCEPTION: main
08-06 15:06:58.341: E/AndroidRuntime(26462): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { (has extras) }} to activity {net.londatiga.android/net.londatiga.android.MainActivity}: java.lang.NullPointerException
08-06 15:06:58.341: E/AndroidRuntime(26462): at android.app.ActivityThread.deliverResults(ActivityThread.java:2994)
08-06 15:06:58.341: E/AndroidRuntime(26462): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3037)
08-06 15:06:58.341: E/AndroidRuntime(26462): at android.app.ActivityThread.access$1100(ActivityThread.java:128)
08-06 15:06:58.341: E/AndroidRuntime(26462): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
08-06 15:06:58.341: E/AndroidRuntime(26462): at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 15:06:58.341: E/AndroidRuntime(26462): at android.os.Looper.loop(Looper.java:137)
08-06 15:06:58.341: E/AndroidRuntime(26462): at android.app.ActivityThread.main(ActivityThread.java:4514)
08-06 15:06:58.341: E/AndroidRuntime(26462): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 15:06:58.341: E/AndroidRuntime(26462): at java.lang.reflect.Method.invoke(Method.java:511)
08-06 15:06:58.341: E/AndroidRuntime(26462): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
08-06 15:06:58.341: E/AndroidRuntime(26462): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
08-06 15:06:58.341: E/AndroidRuntime(26462): at dalvik.system.NativeStart.main(Native Method)
08-06 15:06:58.341: E/AndroidRuntime(26462): Caused by: java.lang.NullPointerException
08-06 15:06:58.341: E/AndroidRuntime(26462): at java.io.File.fixSlashes(File.java:185)
08-06 15:06:58.341: E/AndroidRuntime(26462): at java.io.File.<init>(File.java:134)
08-06 15:06:58.341: E/AndroidRuntime(26462): at net.londatiga.android.MainActivity.saveBitmapToFile(MainActivity.java:128)
08-06 15:06:58.341: E/AndroidRuntime(26462): at net.londatiga.android.MainActivity.onActivityResult(MainActivity.java:112)
08-06 15:06:58.341: E/AndroidRuntime(26462): at android.app.Activity.dispatchActivityResult(Activity.java:4746)
08-06 15:06:58.341: E/AndroidRuntime(26462): at android.app.ActivityThread.deliverResults(ActivityThread.java:2990)
08-06 15:06:58.341: E/AndroidRuntime(26462): ... 11 more
THE CODE:
package net.londatiga.android;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Path;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Uri mImageCaptureUri;
private ImageView mImageView;
private String path;
private Bitmap bitmap;
private static final int PICK_FROM_CAMERA = 1;
private static final int CROP_FROM_CAMERA = 2;
private static final int PICK_FROM_FILE = 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String [] items = new String [] {"Take from camera", "Select from gallery"};
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Image");
builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int item ) { //pick from camera
if (item == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
} else { //pick from file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
}
} );
final AlertDialog dialog = builder.create();
Button button = (Button) findViewById(R.id.btn_crop);
mImageView = (ImageView) findViewById(R.id.iv_photo);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
switch (requestCode) {
case PICK_FROM_CAMERA:
doCrop();
break;
case PICK_FROM_FILE:
mImageCaptureUri = data.getData();
doCrop();
break;
case CROP_FROM_CAMERA:
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
saveBitmapToFile("/sdcard/cropped_img.jpg", photo);
mImageView.setImageBitmap(photo);
}
File f = new File(mImageCaptureUri.getPath());
if (f.exists()) f.delete();
break;
//
}
}
private boolean saveBitmapToFile(String string, Bitmap photo) {
File file = new File(path);
boolean res = false; if (!file.exists())
{
try
{
FileOutputStream fos = new FileOutputStream(file);
res = bitmap.compress(CompressFormat.JPEG, 100, fos); fos.close();
} catch (Exception e)
{ }
} return res;
}
// }
private void doCrop() {
final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
if (size == 0) {
Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
return;
} else {
intent.setData(mImageCaptureUri);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
if (size == 1) {
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
startActivityForResult(i, CROP_FROM_CAMERA);
} else {
for (ResolveInfo res : list) {
final CropOption co = new CropOption();
co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
co.appIntent= new Intent(intent);
co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
cropOptions.add(co);
}
CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose Crop App");
builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int item ) {
startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
}
});
builder.setOnCancelListener( new DialogInterface.OnCancelListener() {
public void onCancel( DialogInterface dialog ) {
if (mImageCaptureUri != null ) {
getContentResolver().delete(mImageCaptureUri, null, null );
mImageCaptureUri = null;
}
}
} );
AlertDialog alert = builder.create();
alert.show();
}
}
}
}
You are getting NPE . See this line bitmap.compress(CompressFormat.JPEG, 100, fos); fos.close(); . is bitmap variable assigned an y value. I dont see it in your code. instread try photo..compress(CompressFormat.JPEG, 100, fos);
There are many bugs in your code.Above mentioned is one.
I just saw that you are getting an exception at File file = new File(path);this variable path is not defined. Initialise it with your path value.
这篇关于在故障提供CameraApplication resultInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!