2012-07-23 44 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

相关问题