簡體   English   中英

捕獲或選擇圖像后,該圖像未顯示在其他活動中嗎?

[英]Image not displaying in another activity after capturing or selecting it?

在我的應用程序中,我必須從圖庫中捕獲或選擇圖像。 如果我捕獲未在其他活動中顯示的圖像並且將我帶到主要活動而圖像未顯示,我該如何解決。 我使用android:largeHeap="true"通過intents傳輸大尺寸圖像。在某些設備中,我無法選擇照片。如果選擇照片,則顯示空指針異常。 。 請讓我得到適當的輸出?

//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);

    }

試試這個,希望對您有幫助。

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

您的問題似乎是一個處理問題,因為您要求在捕獲圖像后立即顯示圖像,但可能尚未保存,因此進入主活動后,只需等待一小段時間(例如700毫秒),然后再顯示圖像即可,在請求之前,先給它指定保存時間。

@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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM