wuzg1020 2016-07-22 05:47 采纳率: 5.6%
浏览 1824

从相册中选择照片,点击了照片后不能显示在ImageView中

我在程序中调用了摄像头拍照和从相册中选择照片,在主程序中点takePhoto按钮依次调用摄像机和剪裁程序,最后在主程序中显示照片。在主程序中点Choose From Album按钮则只显示查找手机中的图片,选中后既不能剪裁也不能显示到主界面。
package com.example.choosepictest;

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.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

public static final int TAKE_PHOTO=1;
public static final int CROP_PHOTO=2;
private Button takePhoto;
private Button chooseFromAlbum;
private ImageView picture;
private Uri imageUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    takePhoto=(Button)findViewById(R.id.take_photo);
    picture=(ImageView)findViewById(R.id.picture);
    takePhoto.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            File outputImage=new File(Environment.getExternalStorageDirectory(),"tempImage.jpg");
            try{
                if(outputImage.exists()){
                    outputImage.delete();
                }
                outputImage.createNewFile();
            }catch(IOException e){
                e.printStackTrace();
            }
            imageUri=Uri.fromFile(outputImage);
            Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(intent, TAKE_PHOTO);
        }
    });
    chooseFromAlbum=(Button)findViewById(R.id.choose_from_album);
    chooseFromAlbum.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // 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){
                e.printStackTrace();
            }
            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) {
    // TODO Auto-generated method stub
    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);
            startActivityForResult(intent, CROP_PHOTO);
        }

        break;
    case CROP_PHOTO:
        if(resultCode==RESULT_OK){
            try{
                Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                picture.setImageBitmap(bitmap);

            }catch(FileNotFoundException e){
                e.printStackTrace();
            }




        }

    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;
}

}

图片说明
配置文件:
<?xml version="1.0" encoding="utf-8"?>
package="com.example.choosepictest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.choosepictest.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

  • 写回答

1条回答 默认 最新

  • sinat_19555777 2016-07-22 06:11
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化