1

我在 onActivityResult 中使用以下代码,但它不起作用

File file1 = new File( Environment.getExternalStorageDirectory().getAbsolutePath(),imagecapture);

            if (file1.exists()) {
              //Do action
                image=(ImageView)findViewById(R.id.image);
             Bitmap myBitmap = BitmapFactory.decodeFile(file1.getAbsolutePath());
          image.setImageBitmap(myBitmap);
       image.invalidate();



            }

但我无法理解为什么它不起作用我也使用 了 Android Camera Intent: how to get full-sized photo? 但它在线上给出错误mImageUri = Uri.fromFile(photo); 任何帮助将不胜感激。

4

2 回答 2

2
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    Button btnTakePhoto;
    ImageView imgTakenPhoto;
    private static final int CAM_REQUREST = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnTakePhoto = (Button) findViewById(R.id.button1);
        imgTakenPhoto = (ImageView) findViewById(R.id.imageView1);

        btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

          if (requestCode == CAMERA_PIC_REQUEST) {
              Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
              imgTakenPhoto.setImageBitmap(thumbnail);
          }
    }

    class btnTakePhotoClicker implements Button.OnClickListener
    {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
        }
    }
}
于 2012-06-05T07:40:26.047 回答
0
File root=new File(Environment.getExternalStorageDirectory()
                + File.separator + "MYPICS" + File.separator);
File sdImageMainDirectory ;
if (!root.exists()) {
            root.mkdirs();
        }
ImageButton cam =(ImageButton)findViewById(R.id.ImageButton1);
cam.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                imageName = "MyPic" + i + ".png";
                sdImageMainDirectory = new File(root, imageName);

                Uri imgUri = Uri.fromFile(sdImageMainDirectory);

                Intent intent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);

                startActivityForResult(intent, 0);


            }
        });




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
        case 0:
            // finish();
            break;

        case -1:
            files = root.listFiles();
            count = files.length;

            try {
                               BitmapFactory.Options option = new BitmapFactory.Options();
                    option.inSampleSize = 2;
            bm = BitmapFactory.decodeFile(root.getPath()+"/"+imageName,option); 

                   ImageView imageview =(ImageView)findViewById(R.id.imageView1);
                    imageview .setImageBitmap(bm);
    }

            } catch (Exception e) {
                e.printStackTrace();
            }
}
于 2012-06-05T06:36:31.240 回答