开发者

problem with my feel for HTC android application

I want to create an application that takes a picture and redisplays the application. But my feeling with my htc code does not work when I tested it on a samsung and it works.

My code:

public class CameraDemo extends Activity {

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        final Button cameraButton = (Button) findViewById(R.id.button1); 
        cameraButton.setOnClickListener( new View.OnClickListener(){ 
         public void onClick(View v ){ 
          Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
          startActivityForResult(intent, 0); 
         } 
        }); 
    } 

    @Override 
    prot开发者_如何学JAVAected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        if (requestCode== 0 && resultCode == Activity.RESULT_OK){ 
            final ImageView iv = (ImageView) findViewById(R.id.imageView1);
            iv.setImageBitmap((Bitmap) data.getExtras().get("data"));
        } 
    }  
} 

Can you help me understand why it does not work on my htc sensation while it works on another phone.

THANKS.


I came across this problem when working with HTC camera too.

From what I recall HTC / Sense handles the return photo slightly differently, here's some pseudo of how I did it to hopefully handle the two variants of handling photos..

public static void StartCameraActivity()
{
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
        // Create the directory if it's not there
        File photoDir = new File(Environment.getExternalStorageDirectory() + "/.pics/");
        photoDir.mkdirs();

        // Construct the file..
        String fileName = File.separator + ".pics/photo" + String.valueOf(System.currentTimeMillis()) + ".jpg";
        File file = new File(Environment.getExternalStorageDirectory(), fileName);
         // Create the intent and remember the place we asked for the file to be placed
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, LinearLayout.VERTICAL );

        _outputFileUri = Uri.fromFile(file);
        context.getActivity().startActivityForResult(intent, 1);
    }

    Bitmap bm = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = SAMPLE_SIZE;
        try
        {
            bm= (Bitmap) data.getExtras().get("data");
            FileOutputStream out = new FileOutputStream(_outputFileUri.getPath());
            bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
        }
        catch (NullPointerException ex)
        {
            bm = OtherImageProcessing(options);
        }
        catch (Exception e)
        {
            throw new Exception("Problem occured.", e);
        }

public static Bitmap OtherImageProcessing(BitmapFactory.Options options) throws Exception
{
    Bitmap bm = null;

    try
    {
        FileInputStream fis = new FileInputStream(_outputFileUri.getPath());
        BufferedInputStream bis = new BufferedInputStream(fis);
        bm = BitmapFactory.decodeStream(bis, null, options);

        // cleaning up
        fis.close();
        bis.close();
    }
    catch (Exception e)
    {
        throw new Exception("Problem", e);
    }

    return bm;
}

Hope that helps...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜