捕获或选择后,图像不会显示在其他活动中?

时间:2015-11-11 08:26:43

标签: java android image android-gallery

在我的应用程序中,我必须从图库中捕获或选择图像。如果我捕获未在另一个活动中显示的图像,它会让我进入主要活动并且图像不显示。如何解决这个问题。 我使用android:largeHeap="true"通过意图传输大尺寸图像。在某些设备中,我无法选择照片。如果我选​​择照片。它显示空指针异常。但它在我的手机中工作正常。 Plsease建议我得到适当的输出?

//camera button click
            Button photobutton=(Button) dialog.findViewById(R.id.camera);
            mimageView=(ImageView) dialog.findViewById(R.id.imageView2);      
            photobutton.setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent takePictureIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if(takePictureIntent.resolveActivity(getPackageManager())!=null)
                    {
                        File PhotoFile=null;
                        try
                        {
                            PhotoFile=createImageFile();
                        }
                        catch(IOException ex)
                        {
                            ex.printStackTrace();
                        }
                        if(PhotoFile!=null)
                        {
                            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(PhotoFile));
                            startActivityForResult(takePictureIntent, 100);
                        }

                    }
                    dialog.dismiss();
                }
            });

//open photos button click
            Button openphotos=(Button)dialog.findViewById(R.id.openphotos);
            openphotos.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent imagegallery = new Intent();
                    imagegallery.setType("image/*");
                    imagegallery.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(imagegallery,"Select Picture"), SELECT_PICTURE);
                    dialog.dismiss();
                }

protected void onActivityResult(int requestcode,int resultcode,Intent data)
{
    if(requestcode==CAMERA_REQUEST&&resultcode==RESULT_OK)
    {
        try
        {
        setPic();
        upload();
        }
        catch(Exception ed)
        {

        }
    }
    else
    {

          Uri selectedImageUri = data.getData();
          mCurrentPhotoPath = getPath(selectedImageUri);
          //Log.e("Image Path : ",  mCurrentPhotoPath);
          setPic();
          upload();
    }
}

private void setPic() {
    // TODO Auto-generated method stub
        int targetw=mimageView.getWidth();
        int targeth=mimageView.getHeight();
        BitmapFactory.Options bmoptions=new BitmapFactory.Options();
        bmoptions.inJustDecodeBounds=true;
        BitmapFactory.decodeFile(mCurrentPhotoPath, bmoptions);
        int photow=bmoptions.outWidth;
        int photoh=bmoptions.outHeight;     
        int scaleFactor=Math.min(photow/targetw, photoh/targeth);
        bmoptions.inJustDecodeBounds=false;
        bmoptions.inSampleSize=scaleFactor;
        bmoptions.inPurgeable=true;
        bitmapscale=BitmapFactory.decodeFile(mCurrentPhotoPath,bmoptions);
        //bitmap = ShrinkBitmap(mCurrentPhotoPath, 100, 100);
        //image.setImageBitmap(bm);

        //mimageView.setImageBitmap(bitmap);    
}
    private void upload()
    {
        Intent i = getIntent();
        imagid=i.getStringExtra("imgid");
        //Bitmap bm=BitmapFactory.decodeFile(mCurrentPhotoPath);
        //bm = ShrinkBitmap(mCurrentPhotoPath, 100, 100);
        ByteArrayOutputStream bao=new ByteArrayOutputStream();
        bitmapscale.compress(Bitmap.CompressFormat.JPEG,50, bao);
        ba=bao.toByteArray();
        //Intent previewwindow=new Intent(this,Saveconfirm.class);
        //startActivity(previewwindow);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
        sendimg = new Intent(Homescreen.this,Saveconfirm.class);
        sendimg.putExtra("picture", ba);
        sendimg.putExtra("path", mCurrentPhotoPath);            
        sendimg.putExtra("imgid",imagid);
            }
        }, 700);
        startActivity(sendimg);         
        //ba1=Base64.encodeToString(ba, Base64.DEFAULT);
        //new uploadToServer().execute();
    }

    public File createImageFile() throws IOException {
        String timeStamp=new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        imageFileName="JPEG_"+ timeStamp +"_";
        //Log.d("imageFileName","cool"+imageFileName);  
        File storageDir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File image=File.createTempFile(imageFileName, ".jpg",storageDir);
        mCurrentPhotoPath=image.getAbsolutePath();
        //Log.d("Getpath","cool"+mCurrentPhotoPath);    
        return image;

    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);           
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);

    }

2 个答案:

答案 0 :(得分:0)

试试这个,希望这会对你有所帮助。

public static Bitmap decodeSampledBitmapFromResource(String pathName,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathName, options);

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(pathName, options);
}

public static 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;
}

答案 1 :(得分:0)

您的问题似乎是一个处理问题,因为您要求图像在捕获之后显示它可能尚未保存,因此在进入主要活动之后只需等待一段时间,如 700ms 在显示图像之前,这将为您提供在请求之前保存所需的时间。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layout);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                display_image();
            }
        }, 700);
}

或者你可以在重定向到主要活动之前等待那段时间:

 new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        //TODO your_intent_to_go_to_mainActivity
    }
 }, 700);