【问题标题】:Please help me with Android Camera issue请帮我解决Android相机问题
【发布时间】:2012-07-11 03:36:38
【问题描述】:

亲爱的世界安卓开发者。

请帮我解决 android camera API 问题。

我想通过 android 相机拍照并将其设置为 ImageButton 的图像。 如果有媒体存储,则将图像保存到特殊路径。

问题如下。

适用于部分安卓设备,如中兴2.2、三星Galaxy 2.3.3、三星平板4.0.3等。

但它不适用于其他一些设备,例如 2.3.4 LG Droid。

我的意思是捕获的图像没有设置为图像按钮。

(2.3.4 LG Droid 的详细设备信息为: 硬件版本:Rev .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;
       }
    }
}

感谢您的所有帮助。

谢谢。

真诚的。

【问题讨论】:

    标签: android api camera


    【解决方案1】:
    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
          }
    
    
    
      }
    

    试试上面的代码...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-16
      • 2022-11-26
      • 1970-01-01
      • 1970-01-01
      • 2022-12-10
      • 2022-12-17
      • 1970-01-01
      • 2011-11-14
      相关资源
      最近更新 更多