当前位置: 首页 > 知识库问答 >
问题:

在Android中添加图像的原因:无法恢复活动:未能交付结果结果信息{who=null,请求=100,结果=-1,数据=null}

江瀚昂
2023-03-14

请帮我解决这个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*),在该页面中,用户可以取消或写下对图像的描述。当她点击“发送照片按钮”时,她返回主页,照片被发送到服务器,并且在主页中先前添加的图像的底部添加一个图像视图,以便用户可以看到她选择的所有图像。

有时(我不知道这是什么时候发生的,有时是在添加第四张照片时,有时是在第一张照片时!)它会出错!(尤其是当相机处于高分辨率时)。

我压缩照片和所有东西。我做错了什么?这和文件名有关吗?压缩?

我的代码出了什么问题?我不知道!

这里我的代码:

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 freed 619K, 16% free 19947K/23495K, paused 20ms, total 25ms 12-08 17:14:52.003:

D/dalvikvm(12330):GC_FOR_ALLOC freed 942K,15%free 20023K/23495K,暂停20ms,总计20ms 12-08 17:14:52.003:I/dalvikvm heap(12330):将2092136字节分配的堆(frag case)增长到21.958MB 12-08 17:14:52.050:D/dalvikvm(12330):GC_并发释放0K,14%freed 22066K/25543K,暂停12ms 4ms,总计42ms 12-08 17:14:53.940:D/dalvikvm(12330):GC_用于释放所有1021K,18%空闲21045K/25543K,暂停19ms,总计19ms 12-08 17:14:53.948:I/dalvikvm堆(12330):将堆(frag case)增长到22.560MB,用于1675864字节分配12-08 17:14:53.971:D/dalvikvm(12330):GC_用于释放所有字节,17%空闲22681K/277k,暂停21ms,总计21ms 12-08 17:14:53.987:D/dalvikvm(12330):GC_用于分配所有释放的0K,17%的空闲22681K/27207K,暂停19ms,总计19ms 12-08 17:14:53.995:I/dalvikvm堆(12330):将堆(frag case)增长到24.719MB,用于分配2263881字节12-08 17:14:14:54.026:D/dalvikvm(12330):GC_用于分配所有释放的0K,16%的空闲24892K/29447K,暂停31ms,总计31ms 12-08 17:14:54.089:D/dalvikvm(12330):GC_并发释放0K,16%释放24892K/29447K,暂停17ms 14ms,总计62ms 12-08 17:14:55.643:D/dalvikvm(12330):GC_用于释放所有

共有2个答案

钱青青
2023-03-14

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

您必须减小位图的大小,如下所示bitmap originalImage=BitmapFactory。解码文件(mCurrentPhotoPath);int width=原始图像。getWidth()*2/4;int height=原始图像。getHeight()*2/4;照片。setImageBitmap(Bitmap.createScaledBitmap(原始图像、宽度、高度、真值)) 然后它会对你有用

萧凡
2023-03-14

这正是我所期望的。当相机或画廊运行时,Android正在杀死你的进程,因为它需要资源。当用户返回到你的应用程序时,Android已经启动了一个新进程,并正在重新创建你的活动。你需要做的是覆盖onSaveInstanceState()onRestoreInstanceState(),并保存和恢复用户在玩相机或画廊应用程序时需要保存的任何东西。

 类似资料:
  • 我有一个应用程序,它显示一个带有列表视图的对话框。在用户从listview中选择一个选项后,我将使用用户选择的信息更改记录。为此,我考虑使用命令setResult(RESULT\u OK,intent) 但我的代码正在生成以下异常: 我的代码: WorkOrderInfo活动: 有人能告诉我代码出了什么问题吗?谢谢你的关注!!!

  • 问题内容: 我真的很沮丧地解决了我的问题,我有一个使用相机的应用程序,当相机拍摄照片时,它将在活动中显示,当我不使用照片时,将在活动中显示,但是名称和路径文件未显示就像我想要的。我正在使用galaxy选项卡进行编译,我已经按照教程从此处,此处和此处解决了我的问题,但是我的应用程序仍然强制关闭并出错,这是我的代码: 活动结果方法: 这是我的logcat说的: 我真的希望有人可以帮助我解决我的问题,我

  • 在这个问题上有人能帮我吗? 我在打印logcat。但我不能复制这一期。我的用户很少会遇到这个问题。 这是从我的用户的Logcat值复制的,他使用的是Android版本6.0.1 提前道谢。

  • 问题内容: 我在我的搜索框中使用了select2。我从URL获取结果,但是无法从中选择一个选项。我想使用“ product.productName”作为选择后要显示的文本。有什么我错过的东西或我犯的任何错误。我包括了select2.css和select2.min.js,jquery.js 这是我的resut_object 问题答案: 您缺少结果数据的id属性。如果没有,则使选项“不可选择”。 例:

  • 在Struts2应用程序中,我尝试使用Custome结果类型。但是我没有得到任何效果,我的JSP页面图像的动作没有得到调用。而且也没有例外得到。请纠正我哪里做错了。HTTPFox说404,但我在JAVA控制台没有得到任何东西。 HTML: XML:

  • 我想将结果添加到resultset,并使用table显示结果,这是我的java代码 错误显示"未找到列'Janunary'。" 很抱歉我没有获得足够的声誉,所以我将存储过程的结果写如下。 查询代码: