2013-05-06 65 views
1

我正在創建一個自動返回隨機圖像的假相機/圖庫應用程序。我將其綁定到IMTION_CAPTURE意圖過濾器的ACTION_PICK &。自定義圖庫應用程序不會返回到調用應用程序

但對於Gallery(PICK)代碼,在完成調用後,它似乎沒有返回到調用應用程序。

這裏是我的代碼:

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]; 
} 
} 
+0

也許這個答案可以幫助你http://stackoverflow.com/questions/2497205/how-to-return-a-result-startactivityforresult-from-a-tabhost-activity – user2340612 2013-05-06 23:59:19

+0

剛剛試過,但沒有做出區別。 – dannyroa 2013-05-07 00:05:32

+0

我不確定,但我認爲你應該使用呼叫意圖,而不是創建一個新的。 – user2340612 2013-05-07 00:12:24

回答

0

您的代碼看起來不錯:你打它使用startActivityForResult

+0

該應用程序正在從ACTION_PICKT意圖&startActivityForResult中調用。 – dannyroa 2013-05-07 00:23:46

相關問題