bigname22
bigname22
2016-05-20 03:38

imageview无法显示相机拍出来的照片

理想中程序:该应用有两个按钮,一个imageview。
一个按钮是调用手机相机拍照之后,剪裁该图片,然后显示在imageview中,但执行完剪裁之后并没有显示在imageview中。
另外一个按钮是打开相册,选取照片后剪裁图片之后再显示在imageview中。
但实际中出现问题:
1.第一个按钮在剪裁之后没有在imageview中显示。(但确定是已经执行了onActivityResult()方法中的显示imageview的方法了,,因为下面代码中加了一句通知能正常弹出来)
2.第二个按钮在打开相册之后没有进入剪裁图片的界面就直接跳回到主界面那里(但还是没有成功显示imageview)ps:因为书上例子是会进入剪裁界面的,但从代码里面看,我并不理解哪一句代码才是进入到剪裁界面。(第一个按钮会出现剪裁我知道)
下面就直接上代码了。就一个类!麻烦大家帮忙看看,感激不尽!!

 package com.example.choosepictext;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
    Button takephotobutton , choosephotobutton ;
    ImageView imageview ;
    private Uri imageuri ;
    public static final int TAKE_PHOTO = 1 ;
    public static final int CROP_PHOTO = 2 ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        takephotobutton = (Button)findViewById(R.id.takebutton);
        choosephotobutton = (Button)findViewById(R.id.choosebutton);
        imageview = (ImageView)findViewById(R.id.imageview);
        takephotobutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
                File outputimage = new File(Environment.getExternalStorageDirectory(), "tempImage.jpg");
                try{
                    if(outputimage.exists()){
                        outputimage.delete();
                    }
                    outputimage.createNewFile();
                }catch(Exception e){}
                imageuri = Uri.fromFile(outputimage);//吧outputimage转成uri对象
                Log.v("Mylog","buttononclick:"+imageuri.getPath());
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");//调用相机
                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageuri);
                startActivityForResult(intent, TAKE_PHOTO);
            }
        }
        );
        choosephotobutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
                File outputimage = new File(Environment.getExternalStorageDirectory(),"output_image.jpg");
                try{
                    if(outputimage.exists()){
                        outputimage.delete();
                    }
                    outputimage.createNewFile();
                }catch(IOException e )  {}
                imageuri = Uri.fromFile(outputimage);
                Intent intent = new Intent("android.intent.action.GET_CONTENT");
                intent.setType("image/*");
                intent.putExtra("crop", true);
                intent.putExtra("scale", true);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageuri);
                startActivityForResult(intent, CROP_PHOTO);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode , int resultCode, Intent data){
        switch(requestCode){
        case TAKE_PHOTO:
            if(resultCode == RESULT_OK){
            Intent intent = new Intent("com.android.camera.action.CROP");//调用剪裁
            intent.setDataAndType(imageuri,"image/*");
            intent.putExtra("scale", true);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageuri);
            Log.v("Mylog","result:"+imageuri.getPath());
            startActivityForResult(intent, CROP_PHOTO);
            }
            break ;
        case CROP_PHOTO:

            try{
                Log.v("Mylog","crop:"+imageuri.getPath());
                Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageuri));
                BitmapFactory.decodeFile(imageuri.getPath());
                imageview.setImageBitmap(bitmap);
                imageview.setVisibility(View.VISIBLE);
                Toast.makeText(MainActivity.this, "setImage", Toast.LENGTH_SHORT).show();

            }catch(FileNotFoundException e){}

                break ;
            default:
                break ;
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

5条回答