首页 文章

在Android中添加图片原因:无法恢复活动:失败传递结果ResultInfo {who = null,request = 100,result = -1,data = null}

提问于
浏览
3

请帮我解决这个Android相机 .

我得到的错误如下:

Unable to resume activity {..,CreatePropertyActivity5}: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity..: java.lang.NullPointer

它也给出了这个警告,因为我压缩图像是很奇怪的:

W/OpenGLRenderer(10242): Bitmap too large to be uploaded into a texture (3264x2448, max=2048x2048)

我的应用程序允许用户从图库中选择照片或使用相机拍摄照片 . 正如您可以从代码中弄清楚的那样,用户添加的照片数量没有限制 .

在主页面(createPropertyActivity * 5 *)中,用户触摸拍摄照片按钮,她选择相机或图库,然后她选择(或拍摄)图片,然后应用程序转到页面(CreatePropertyActivity * 51 *),其中她可以取消或写下图像的描述 . 当她触摸“发送照片按钮”时,她返回主页面,将照片发送到服务器,并将图像视图添加到主页面中先前添加的图像的底部,以便用户可以看到她的所有图像选择了 .

有时候(我不知道什么时候会发生这种情况,有时是第4张照片的添加时间,有时甚至是第一张照片!)它会出错! (特别是当相机处于高分辨率时) .

我压缩照片和一切 . 我究竟做错了什么?它与文件名有什么关系吗?压缩?

我的代码有什么问题?我没有线索!

这是我的代码:

public class CreatePropertyActivity5 extends ActionBarActivity {
protected static final int SELECT_PICTURE = 1;
private static final int ACTIVITY_REQUEST_CODE_IMAGE = 100;
private static final int IMAGE_DESCRIPTION = 200;
LinearLayout ll; 
private List<File> cameraImageFiles;
private JSONRequestForCreatePropertyListing propertyListing;
ImageView imageView;
Bitmap selectedImageBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_property_5);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    propertyListing = (JSONRequestForCreatePropertyListing) getIntent().getSerializableExtra("JSONRequestForCreatePropertyListing");
    CreatePropertListingAsync cplp = new CreatePropertListingAsync(this, propertyListing);
    cplp.execute();

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.create_property_activity5, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void onClickTakePicture(View v) throws IOException {
    // Camera.
    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);     
    final PackageManager packageManager = getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);


    cameraImageFiles = new ArrayList<File>();

    int i=0;
    for(ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.MEDIA_IGNORE_FILENAME, ".nomedia");

        //** below 4 lines put the uri of the camera taken picture to the EXTRA_OUTPUT 
        File cameraImageOutputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myFileName");
        cameraImageFiles.add(cameraImageOutputFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraImageFiles.get(i)));
        i++;

        cameraIntents.add(intent);
    }

    // Filesystem.
    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

    // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(galleryIntent, "add new");

    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
    startActivityForResult(chooserIntent, ACTIVITY_REQUEST_CODE_IMAGE);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent)
{
    ll = (LinearLayout) findViewById(R.id.llCreatePropertyImages);

    switch(requestCode) { 

    // For sending photos to server. We come here from activity51
    case IMAGE_DESCRIPTION:
        if(resultCode == RESULT_OK){
            //add the image to the activity5 page.
            imageView = new ImageView(this);
            imageView.setPadding(0, 10, 0, 0);
            ll.setVisibility(View.VISIBLE);
            imageView.setImageBitmap(selectedImageBitmap);
            ll.addView(imageView);




            String s = imageReturnedIntent.getStringExtra("key");
            //user entered description is in "key"
            imageView.setTag(s);
            Bitmap bitmap1 = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
            ByteArrayOutputStream stream=new ByteArrayOutputStream();
            bitmap1.compress(Bitmap.CompressFormat.PNG, 90, stream);
            byte[] image=stream.toByteArray();
            String img_str = Base64.encodeToString(image, 0);

            //This part sends the picture to the server
            ArrayList<Photos> photos = new ArrayList<Photos>();
            photos.add(new Photos(new Ax(img_str)));

            int id = Integer.parseInt((String) ((TextView) findViewById(R.id.txt_property_listing_ID)).getText());
            int editPass = Integer.parseInt((String) ((TextView) findViewById(R.id.txt_property_listing_password)).getText());
            JSONRequestForAddPhoto jr = new JSONRequestForAddPhoto(id, editPass, photos);

            new AddPhotoAsync(this, jr).execute();




        }
        break;
    //For choosing photos from gallery or taking one with camera
    case ACTIVITY_REQUEST_CODE_IMAGE:
        if(resultCode == RESULT_OK){

            Uri uri = null;
            if(imageReturnedIntent == null){   //since we used EXTRA_OUTPUT for camera, so it will be null

                for(int i=0;i<cameraImageFiles.size();i++){
                    if(cameraImageFiles.get(i).exists()){
                        uri = Uri.fromFile(cameraImageFiles.get(i));
                        break;
                    }
                }
            }
            else {  // from gallery
                uri = imageReturnedIntent.getData();
            }

            if(uri != null){
                try {
                    Bitmap bitmap3 = decodeSampledBitmapFromResource(uri, 500, 500);
                    selectedImageBitmap = bitmap3;

                    Intent i= new Intent(this, CreatePropertyActivity51.class);
                    i.putExtra("photoUri", uri);
                    startActivityForResult(i,IMAGE_DESCRIPTION);

                    //*** show activity51

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

public Bitmap decodeSampledBitmapFromResource(Uri uri, int reqWidth, int reqHeight) throws IOException {

    ContentResolver cr = getContentResolver();
    InputStream inStream = cr.openInputStream(uri);
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(inStream, null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    inStream.close();
    inStream = cr.openInputStream(uri);


    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap result = BitmapFactory.decodeStream(inStream, null , options);
    inStream.close();
    return result;
}
public int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}
}

第二项活动:

public class CreatePropertyActivity51 extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_property_51);
    ImageView imageView = (ImageView) findViewById(R.id.img_selected_image);
    Uri uri = getIntent().getParcelableExtra("photoUri");
    Bitmap bitmap3;
    try {
        bitmap3 = decodeSampledBitmapFromResource(uri, 200, 200);
        imageView.setImageBitmap(bitmap3);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.create_property_activity51, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void sendPhoto(View v){
    Intent data = new Intent();
    data.putExtra("key", ((EditText)findViewById(R.id.etxt_photo_description)).getText().toString());
    setResult(Activity.RESULT_OK, data);
    finish();
}
public void cancelSendPhoto(View v){
    Intent data = new Intent();
    setResult(Activity.RESULT_CANCELED, data);
    finish();
}


public Bitmap decodeSampledBitmapFromResource(Uri uri, int reqWidth, int reqHeight) throws IOException {

    ContentResolver cr = getContentResolver();
    InputStream inStream = cr.openInputStream(uri);
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(inStream, null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    inStream.close();
    inStream = cr.openInputStream(uri);


    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap result = BitmapFactory.decodeStream(inStream, null , options);
    inStream.close();
    return result;
}
public int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}
}

logcat的:

12-08 17:14:50.448:D / dalvikvm(12330):GC_FOR_ALLOC释放619K,16%免费19947K / 23495K,暂停20ms,总计25ms 12-08 17:14:52.003:
D / dalvikvm(12330):GC_FOR_ALLOC释放942K,15%免费20023K / 23495K,暂停20ms,总计20ms 12-08 17:14:52.003:I / dalvikvm-heap(12330):增长堆(frag case)至21.958MB for 2092136-byte allocation 12-08 17:14:52.050:D / dalvikvm(12330):GC_CONCURRENT释放0K,14%释放22066K / 25543K,暂停12ms 4ms,总计42ms 12-08 17:14:53.940:D / dalvikvm (12330):GC_FOR_ALLOC释放1021K,18%免费21045K / 25543K,暂停19ms,总计19ms 12-08 17:14:53.948:I / dalvikvm-heap(12330):将堆(frag case)增长到22.560MB for 1675864-字节分配12-08 17:14:53.971:D / dalvikvm(12330):GC_FOR_ALLOC释放0K,17%释放22681K / 27207K,暂停21ms,总计21ms 12-08 17:14:53.987:D / dalvikvm(12330): GC_FOR_ALLOC释放0K,17%释放22681K / 27207K,暂停19ms,总计19ms 12-08 17:14:53.995:I / dalvikvm-heap(12330):将堆(frag case)增长到24.719MB,用于2263881字节分配12- 08 17:14:54.026:D / dalvikvm(12330):GC_FOR_ALLOC释放0K,16%免费24892K / 29447K,暂停31ms,总计31ms 12-08 17:14:54.089:D / dalvikvm(1 2330):GC_CONCURRENT释放0K,16%空闲24892K / 29447K,暂停17ms 14ms,总计62ms 12-08 17:14:55.643:D / dalvikvm(12330):GC_FOR_ALLOC释放<1K,16%释放24893K / 29447K,暂停20ms ,总共20ms 12-08 17:14:55.667:I / dalvikvm-heap(12330):将堆(frag case)增长到29.039MB,用于4527746字节分配12-08 17:14:55.721:D / dalvikvm(12330) :GC_CONCURRENT释放0K,14%免费29315K / 33927K,暂停13ms 7ms,总计51ms 12-08 17:14:55.721:D / dalvikvm(12330):WAIT_FOR_CONCURRENT_GC阻止20ms 12-08 17:14:55.737:I / Choreographer( 12330):跳过652帧!应用程序可能在其主线程上做了太多工作 . 12-08 17:14:55.987:D / SensorManager(12330):unregisterListener :: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi $ KfukwiFmfulTrklfufiRvht @,)ad)fb(12-08 17:14:55.987:D / Sensors(12330):Remain listener =发送..正常延迟200ms 12-08 17:14:55.987:I / Sensors(12330):sendDelay --- 200000000 12-08 17:14:55.987:D / SensorManager(12330):JNI - sendDelay 12-08 17 :14:55.987:I / SensorManager(12330):设置正常延迟=真12-08 17:14:56.120:W / IInputConnectionWrapper(12330):getSelectedText处于非活动状态InputConnection 12-08 17:14:56.120:W / IInputConnectionWrapper( 12330):setComposingText on inactive InputConnection 12-08 17:14:56.120:W / IInputConnectionWrapper(12330):getExtractedText on inactive InputConnection 12-08 17:14:56.643:D / dalvikvm(12330):GC_CONCURRENT释放6893K,28%免费24437K / 33927K,暂停12ms 16ms,总计64ms 12-08 17:14:57.010:D / dalvikvm(12330):GC_FOR_ALLOC释放1102K,29%免费24331K / 33927K,暂停25ms,总计25ms 12-08 17:14:57.425 :D / dalvikvm(12330):GC_FOR_ALL OC释放994K,27%免费24781K / 33927K,暂停21ms,总计21ms 12-08 17:14:57.464:D / dalvikvm(12330):GC_CONCURRENT释放5K,21%免费26807K / 33927K,暂停12ms 4ms,总计35ms 12 -08 17:14:57.964:D / AbsListView(12330):获取MotionRecognitionManager 12-08 17:14:58.120:D / dalvikvm(12330):GC_FOR_ALLOC已释放1498K,25%免费25505K / 33927K,暂停39ms,总计39ms 12-08 17:14:58.135:I / dalvikvm-heap(12330):增长堆(frag case)为28.284MB,3110734字节分配12-08 17 :14:58.167:D / dalvikvm(12330):GC_FOR_ALLOC释放1K,23%空闲28541K / 36999K,暂停32ms,总计32ms 12-08 17:14:58.190:W / ResourceType(12330):失败进入0x010802c0(包0中的t = 7 e = 704)(错误-75)12-08 17:14:58.214:D / dalvikvm(12330):GC_CONCURRENT释放2034K,29%释放26521K / 36999K,暂停16ms 7ms,总计45ms 12- 08 17:14:59.245:D / dalvikvm(12330):GC_FOR_ALLOC释放215K,29%免费26591K / 36999K,暂停22ms,总计22ms 12-08 17:14:59.245:I / dalvikvm-heap(12330):增长堆(frag case)为30.828MB for 4666096-byte allocation 12-08 17:14:59.300:D / dalvikvm(12330):GC_CONCURRENT释放1K,16%释放31146K / 36999K,暂停32ms 4ms,总计57ms 12-08 17: 14:59.300:D / dalvikvm(12330):WAIT_FOR_CONCURRENT_GC阻塞24ms 12-08 17:15:00.495:W / IInputConnectionWrapper(12330):showStatusIcon on inactive输入连接12-08 17:15:01.526:D / dalvikvm(12330):GC_FOR_ALLOC释放3339K,25%空闲28107K / 36999K,暂停22ms,总计24ms 12-08 17:15:01.526:D / AbsListView(12330):[ unregisterDoubleTapMotionListener] 12-08 17:15:01.534:I / dalvikvm-heap(12330):将堆(frag case)增长到32.233MB,用于4586988字节分配12-08 17:15:01.604:D / dalvikvm(12330): GC_FOR_ALLOC释放2K,22%空闲32584K / 41479K,暂停71ms,总计71ms 12-08 17:15:01.620:I / MotionRecognitionManager(12330):. unregisterListener:/ listener count = 0-> 0,ubvf 9budiwrd5ordgfl5BakTrklMrfo $,@, )aa):88 12-08 17:15:01.643:D / dalvikvm(12330):GC_CONCURRENT释放18K,22%免费32567K / 41479K,暂停13ms 4ms,总计40ms 12-08 17:15:01.643:D / dalvikvm (12330):WAIT_FOR_CONCURRENT_GC阻止26ms 12-08 17:15:01.690:D / dalvikvm(12330):GC_FOR_ALLOC释放4657K,33%免费27910K / 41479K,暂停45ms,总计45ms 12-08 17:15:01.690:I / dalvikvm-heap(12330):将堆(frag case)增长到29.853MB,用于2293502字节分配12-08 17:15:01.729:D / dalv ikvm(12330):GC_CONCURRENT释放0K,28%免费30150K / 41479K,暂停4ms 4ms,总计39ms 12-08 17:15:01.729:D / dalvikvm(12330):WAIT_FOR_CONCURRENT_GC阻塞6ms 12-08 17:15:23.596: D / AndroidRuntime(12330):关闭VM 12-08 17:15:23.596:W / dalvikvm(12330):threadid = 1:线程退出时未捕获异常(组= 0x41d0e2a0)12-08 17:15:23.635:E / AndroidRuntime(12330):FATAL EXCEPTION:main 12-08 17:15:23.635:E / AndroidRuntime(12330):java.lang.RuntimeException:无法恢复活动{com.appName.appName / com.appName.appName.CreatePropertyActivity5 }:java.lang.RuntimeException:将结果ResultInfo {who = null,request = 100,result = -1,data = null}传递给activity {com.appName.appName / com.appName.appName.CreatePropertyActivity5}:java . lang.NullPointerException 12-08 17:15:23.635:E / AndroidRuntime(12330):at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2636)12-08 17:15:23.635:E / AndroidRuntime(12330):在android.app.ActivityThread.handleResumeActi vity(ActivityThread.java:2664)12-08 17:15:23.635:E / AndroidRuntime(12330):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2137)12-08 17:15:23.635:E / AndroidRuntime(12330):在android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3573)12-08 17:15:23.635:E / AndroidRuntime(12330):在android.app.ActivityThread.access $ 800(ActivityThread.java: 140)12-08 17:15:23.635:E / AndroidRuntime(12330):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1244)12-08 17:15:23.635:E / AndroidRuntime(12330) :在android.os.Handler.dispatchMessage(Handler.java:99)12-08 17:15:23.635:E / AndroidRuntime(12330):在android.os.Looper.loop(Looper.java:137)12-08 17:15:23.635:E / AndroidRuntime(12330):在android.app.ActivityThread.main(ActivityThread.java:4918)12-08 17:15:23.635:E / AndroidRuntime(12330):at java.lang.reflect .Method.invokeNative(Native Method)12-08 17:15:23.635:E / AndroidRuntime(12330):at java.lang.reflect.Method.invoke(Method.java:511)12- 08 17:15:23.635:E / AndroidRuntime(12330):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:994)12-08 17:15:23.635:E / AndroidRuntime(12330) :at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)12-08 17:15:23.635:E / AndroidRuntime(12330):at dalvik.system.NativeStart.main(Native Method)12- 08 17:15:23.635:E / AndroidRuntime(12330):引起:java.lang.RuntimeException:未能将结果ResultInfo {who = null,request = 100,result = -1,data = null}传递给activity {com.appName.appName / com.appName.appName.CreatePropertyActivity5}:java.lang.NullPointerException 12-08 17:15:23.635:E / AndroidRuntime(12330):at android.app.ActivityThread.deliverResults(ActivityThread.java :3202)12-08 17:15:23.635:E / AndroidRuntime(12330):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2623)12-08 17:15:23.635:E / AndroidRuntime(12330): ... 13更多12-08 17:15:23.635:E / AndroidRuntime(12330):引起:java.lang.NullPointerException 12-08 17:15:23.635:E / AndroidRuntime(12330):at com.appName . appName.CreatePropertyActivity5.onActivityResult(CreatePropertyActivity5.java:167)12-08 17:15:23.635:E / AndroidRuntime(12330):at android.app.Activity.dispatchActivityResult(Activity.java:5369)12-08 17:15: 23.635:E / AndroidRuntime(12330):在android.app.ActivityThread.deliverResults(ActivityThread.java:3198)12-08 17:15:23.635:E / AndroidRuntime(12330):... 14更多

2 回答

  • 9

    's what I expected. Android is killing your process while the Camera or Gallery is running because it needs the resources. When the user returns to your app, Android has started a new process and is recreating your activities. What you'll需要做的是覆盖 onSaveInstanceState()onRestoreInstanceState() 并保存和恢复用户正在玩相机或图库应用程序时需要保留的任何内容 .

  • 0

    W / OpenGLRenderer(10242):对于这个问题的答案,位图太大而无法上传到纹理(3264x2448,max = 2048x2048):

    你必须减少位图的大小,如下面 Bitmap originalImage = BitmapFactory.decodeFile(mCurrentPhotoPath); int width = originalImage.getWidth()*2/4; int height = originalImage.getHeight()*2/4; pic.setImageBitmap(Bitmap.createScaledBitmap(originalImage, width, height, true)); 那么它将适合你

相关问题