2012-07-23 45 views
1

我想從我的畫廊拍攝照片,我想給它一個「id」以使用我的android應用程序。如何從畫廊中拍照並在我的應用程序中使用此照片

+0

這個應用程序是否應該在您的手機上運行? – 2012-07-23 09:05:51

+2

有很多類似這樣的帖子...第一搜索 – Manikandan 2012-07-23 09:07:15

+0

可能的重複[如何捕捉圖像並將其與本機Android相機存儲](http://stackoverflow.com/questions/3442462/how-to-捕捉圖像和存儲它與本機安卓相機) – Keppil 2012-07-23 09:13:10

回答

1

在點擊按鈕甚至,請撥打以下代碼....

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(intent, GET_IMAGE); 

,並在活動成果,編寫如下代碼:

if (requestCode == GET_IMAGE) {  
    targetUri = data.getData(); 
    imageView.setImageURI(targetUri);     
} 

其中,private static final int GET_IMAGE = 2;

0

你可以請嘗試下面的代碼

btnChoosePhoto.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent i = new Intent(Intent.ACTION_PICK, 
        android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); 

      startActivityForResult(i, 1); 
     } 
    }); 


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

    super.onActivityResult(requestCode, resultCode, data); 

    if(resultCode == RESULT_OK && requestCode == 1) { 

     final Uri selectedImage = data.getData(); 

     try { 
      bitmap = Media.getBitmap(getContentResolver(),selectedImage); 
      ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes); 

      //path = selectedImage.getEncodedPath(); 

      // create new file to write the encrypted bytes of image in file 
      File f = new File(Environment.getExternalStorageDirectory() 
        + File.separator 
        + filename); 
      f.createNewFile(); 
      FileOutputStream fo = new FileOutputStream(f); 
      fo.write(bytes.toByteArray()); 

     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     imageView.setImageBitmap(bitmap); 

    } 
} 
+0

謝謝你的回答。但我不會將此圖片用作imageView。我想用這張圖片改變其他班級的背景。我可不可以做 ? – 2012-07-23 12:00:04

相關問題