「已注销」 2015-07-30 09:28 采纳率: 0%
浏览 3030

《第一行代码>,从相册取出照片时取不出来?大家看我代码有问题吗?

以下是MainActivity.java的代码
package com.example.administrator.choosepictest;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

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

public class MainActivity extends Activity {

public static final int TAKE_PHOTO = 1;

public static final int CROP_PHOTO = 2;

private Button takePhoto;

private ImageView picture;

private Uri imageUri;

private Button chooseFromAlbum;

@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 View.OnClickListener() {
        @Override
        public void onClick(View v) {
            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.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 View.OnClickListener() {
        @Override
        public void onClick(View v) {
            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) {
    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));
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
                    picture.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            break;
        default:
            break;
    }
}

}

  • 写回答

4条回答 默认 最新

  • gao_chun 博客专家认证 2015-08-01 03:16
    关注

    1.对照例子检查是否有哪句代码写错了
    2.大断点debug一下,跟踪确定哪一行代码没起作用或者为空
    3.参考例子:http://blog.csdn.net/gao_chun/article/details/40864121

    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题