1

I'm creating a Fake Camera/Gallery app that just automatically return a random image. I tied it to the ACTION_PICK & IMAGE_CAPTURE intent filters.

However for the Gallery (PICK) code, after calling finish, it doesn't seem to be returning to the calling app.

Here's my code:

public class MainActivity extends Activity {

final static int[] photos = { R.drawable.android_1, R.drawable.android_2,
        R.drawable.android_3 };
static int lastPhotoIndex = -1;

private final static String TAG = "FakeCameraGallery";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String action = getIntent().getAction();

    if (action.contains("PICK")) {

                    //Act as Gallery

        InputStream in = getResources().openRawResource(getNextPhoto());
        Bitmap bm = BitmapFactory.decodeStream(in);

        Bundle extras = new Bundle();
        extras.putParcelable("data", bm);

        Intent result = getIntent().putExtras(extras);

        if (getParent() == null) {
            setResult(Activity.RESULT_OK, result);
        } else {
            getParent().setResult(Activity.RESULT_OK, result);
        }

    } else {
                    //act as Camera
        prepareToSnapPicture();
    }

    finish();
}

private void prepareToSnapPicture() {
    checkSdCard();
    Intent intent = getIntent();

    if (intent.getExtras() != null) {
        snapPicture(intent);
        setResult(RESULT_OK);
    } else {
        Log.i(TAG, "Unable to capture photo. Missing Intent Extras.");
        setResult(RESULT_CANCELED);
    }
}

private void checkSdCard() {
    if (!Environment.MEDIA_MOUNTED.equals(Environment
            .getExternalStorageState())) {
        Toast.makeText(this, "External SD card not mounted",
                Toast.LENGTH_LONG).show();
        Log.i(TAG, "External SD card not mounted");
    }
}

private void snapPicture(Intent intent) {
    try {
        this.copyFile(getPicturePath(intent));
        Toast.makeText(this, "Click!", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        Log.i(TAG, "Can't copy photo");
        e.printStackTrace();
    }
}

private File getPicturePath(Intent intent) {
    Uri uri = (Uri) intent.getExtras().get(MediaStore.EXTRA_OUTPUT);
    return new File(uri.getPath());
}

private void copyFile(File destination) throws IOException {
    InputStream in = getResources().openRawResource(getNextPhoto());
    OutputStream out = new FileOutputStream(destination);
    byte[] buffer = new byte[1024];
    int length;

    if (in != null) {
        Log.i(TAG, "Input photo stream is null");

        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }

        in.close();
    }

    out.close();
}

private int getNextPhoto() {
    if (lastPhotoIndex == photos.length - 1) {
        lastPhotoIndex = -1;
    }

    return photos[++lastPhotoIndex];
}
}
dannyroa
  • 5,501
  • 6
  • 41
  • 59
  • Maybe this answer can help you http://stackoverflow.com/questions/2497205/how-to-return-a-result-startactivityforresult-from-a-tabhost-activity – user2340612 May 06 '13 at 23:59
  • Just tried that but didn't make a difference. – dannyroa May 07 '13 at 00:05
  • I'm not sure but I think that you should use the calling intent instead of creating a new one. – user2340612 May 07 '13 at 00:12
  • I just used the calling intent & it's the same thing. The app is being launched from a ACTION_PICK intent if that makes a difference. – dannyroa May 07 '13 at 00:20
  • Well the code is right.. Sometimes I do error in calling activity: I confuse "requestCode" with "resultCode" – user2340612 May 07 '13 at 00:32
  • I'm actually creating a Fake Camera & Gallery in one. Check out the updated source code. I don't get why the camera code works without setting a result. – dannyroa May 07 '13 at 00:34

1 Answers1

0

Your code looks fine: are you calling it using startActivityForResult?

Femi
  • 64,273
  • 8
  • 118
  • 148