简体   繁体   中英

Saving photo in Android to sd card and show them in bitmap in other intent

I need a help with this Android application! I'm at very beginning in developing on Android and for a school's project I have to create an App that scan qr's codes and shoot photo.

I have a problem on shooting photo, I'd like to shoot the photo, confirm it pressing "tick button", open a new intent that allow you to add a comment on Image , and then through another button I'll ad inside an ArrayAdapter .

I'll try to show you the parts of the code that have problem, if anything more is needed just ask!

The method TakePhoto is inside a Fragment that launch the camera intent pressing a button.

public void TakePhoto() {

    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

    int imageNum = 0;
    File imagesFolder = new File(Environment.getExternalStorageDirectory(),"aMuse");
    imagesFolder.mkdirs();
    String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
    File output = new File(imagesFolder, fileName);
    while (output.exists()){
        imageNum++;
        fileName = "image_" + String.valueOf(imageNum) + ".jpg";
        output = new File(imagesFolder, fileName);
    }
    Uri uriSavedImage = Uri.fromFile(output);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

    getActivity().startActivityForResult(intent, 100);
}

The onActivityResult is inside a FragmentActivity that contains 2 more Fragment , and I'll need to pass the image shooted to another intent (imageResult) that will show the photo shooted and permit to add comment through an EditText .

public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    super.onActivityResult(requestCode, resultCode, intent); 

    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

    if (scanResult != null) {
        Intent qrResult = new Intent(this, QrResult.class);
        String resultString = scanResult.getContents();
        qrResult.putExtra(EXTRA_MESSAGE, resultString);
        startActivity(qrResult);
    }
    else{
        switch (requestCode) {
        case 100:
            if (resultCode == Activity.RESULT_OK) {
                Intent imageResult = new Intent(this, ImagePreview.class);

                startActivity(imageResult);
            }
        }
    }

In this situation the app (as I understood) save the photo inside SD/aMuse with name image_nn.jpg. What I need to do is get this photo and set inside imageResult as bitmap. I tried passing the uri through .putExtra method but it crash giving "null pointer exception" error.

I think that this blog post I wrote on this matter can be really helpful to you:

Guide: Android: Use Camera Activity for Thumbnail and Full Size Image

The titles explains the content. Go over it and tell me if you have a problem after reading it.

what important to you is the full size image part.

UPDATE: The problem you are facing right now:

 FAILED BINDER TRANSACTION ERROR

is not related to the way you get your image, but to the image it self, quoting android developer:

 The most common reason for this error is a too large IPC

source: https://groups.google.com/forum/#!topic/android-developers/KKEyW6XdDvg/discussion

IPC (stand for: Inter-Process Communication ) is basically the way proccesses in android comunicated, you can read more here:

http://www.slideshare.net/jserv/android-internals-30176596

And the problem you are facing derived most probably baecase your image is too big.

Back to the guide I posted, you have there this peace of code:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{  
//Check that request code matches ours:
if (requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE) 
{
    //Get our saved file into a bitmap object:
   File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
   Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
}

}

for this line of code:

 Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);

try to provide a smaller dimmensions, some thing like:

 Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 100, 70);

see if this fixes your problem, and then you can play with those parameters to get your best result.

In first activity:

fos = new FileOutputStream(pictureFile);
    fos.write(bArray);
        fos.close();
    Intent i = new Intent(ClickImg.this, CameraAct.class);
    imgPath = Uri.fromFile(output);

    System.out.println("Main Data: " +pathFile);
    i.putExtra("bytedata", pathFile);
    i.putExtra("pname", photoName);

    startActivity(i);
    this.finish();

In second activity:

           String photoName = i.getStringExtra("pname");
Bitmap myBitmap = BitmapFactory.decodeFile(photoName);

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(myBitmap);

Here is the code which will capture the photo and will show in image view

package com.android.test;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MyCameraActivity extends Activity {
    private static final int CAMERA_REQUEST = 1888; 
    private ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        photoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_REQUEST); 
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }  
    } 
}

Here is the XML resource:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/photo"></Button>
    <ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>

</LinearLayout>

Don't forget to add the permission:

 <uses-feature android:name="android.hardware.camera"></uses-feature>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

If you received data as null in activity result then use following code:

Code to get high resolution images from camera.

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 /*
 Here destination is the File object in which your captured images will be stored
 */
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
/*
Here REQUEST_IMAGE is the unique integer value you can pass it any integer
*/
startActivityForResult(intent, REQUEST_IMAGE);

After this implement the onActivityResult method like below

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
        // Now check the file which you have pass with the intent to capture.
        // Camera had stored the captured image to the file which you have passed with the intent.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM