2012-07-11 86 views
0

親愛的世界的android開發人員。請幫助我的Android相機問題

請幫助我解決Android相機API問題。

我想通過android相機拍照並將其設置爲ImageButton的圖像。 如果有媒體商店,則將圖像保存到特殊路徑。

問題如下。

它適用於某些Android設備,如中興通訊2.2,三星Galaxy 2.3.3和三星平板4.0.3等

但它不會在其他一些設備如2.3.4 LG Droid上使用。

我的意思是拍攝的圖像沒有設置爲圖像按鈕。

(2.3.4 LG Droid的設備的詳細信息是: 硬件版本:版本.1.1, 軟件版本:ms910zbc, 內部版本號薑餅)

這是我的代碼。

String mPhotoPath; 
ImageButton mPhotoButton; 

mPhotoButton.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     String storageState = Environment.getExternalStorageState(); 
     if (storageState.equals(Environment.MEDIA_MOUNTED)) { 
      File photoFile = new File(mPhotoPath); 
      try { 
       if (!photoFile.exists()) { 
        photoFile.getParentFile().mkdirs(); 
        photoFile.createNewFile(); 
        photoFile.setWritable(true, false); 
       } 
      } catch (IOException e) { 

      } 

      Uri fileUri = Uri.fromFile(photoFile); 
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
      startActivityForResult(intent, TAKE_PICTURE); 
     }else { 
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(intent, TAKE_PICTURE); 
     } 
    } 
}); 



protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode != RESULT_OK) { 
     return; 
    } 
    switch (requestCode) { 
     case TAKE_PICTURE: { 
      if (data != null) { //ZTE 2.2 device 
       try { 
        Bundle extras = data.getExtras(); 
        if (extras != null) 
        { 
         fPhoto = extras.getParcelable("data"); 
         mPhotoButton.setImageBitmap(fPhoto); 
        }      
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
      else //Samsung 2.3.3 device 
      { 
       try { 
        FileInputStream stream = new FileInputStream(new File(mPhotoPath)); 
        fPhoto = BitmapFactory.decodeStream(stream); 
        stream.close(); 
        mPhotoButton.setImageBitmap(fPhoto); 
       }catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
      break; 
     } 
    } 
} 

我感謝你的幫助。

謝謝。

真誠。

回答

0
public class Camera extends Activity 
    { 
private static final int CAMERA_REQUEST = 1888; 
private String selectedImagePath; 
ImageButton mPhotoButton; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    mPhotoButton=(Button)findElementById(R.id.imgbtn1); 
    mPhotoButton.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      Intent cameraIntent = new Intent(ACTION_IMAGE_CAPTURE); 
      startActivityForResult(cameraIntent, CAMERA_REQUEST); 
     } 
    } 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if (resultCode == RESULT_OK) { 
     if (requestCode == CAMERA_REQUEST) 
     { 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
      photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 
      Random randomGenerator = new Random();randomGenerator.nextInt(); 
      String newimagename=randomGenerator.toString()+".jpg"; 
      File f = new File(Environment.getExternalStorageDirectory() 
            + File.separator + newimagename); 
      try { 
       f.createNewFile(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      //write the bytes in file 

      try { 
       fo = new FileOutputStream(f.getAbsoluteFile()); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 
       fo.write(bytes.toByteArray()); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
       uri=f.getAbsolutePath(); 
       mPhotoButton.setImageResource(uri); 
    //this is the url that where you are saved the image 
     } 



    } 

試試上面的代碼...

+0

如果它給你的解決方案,接受這個作爲答案 – Ponmalar 2012-07-16 03:50:17