无法通过图像数据传回Android应用程序 [英] Unable To Pass Image Data Back To Android Application

查看:167
本文介绍了无法通过图像数据传回Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图捕捉后的图像数据传回给应用程序。然而,它在试图返回到我的应用程序总是崩溃。

在code,起的意图是:

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    imgView =(ImageView的)findViewById(R.id.ImageView);
    上传=(按钮)findViewById(R.id.Upload);
    捕捉=(按钮)findViewById(R.id.Snap);
    选择=(按钮)findViewById(R.id.Select);
    主题=(EditText上)findViewById(R.id.Subject);
    味精=(EditText上)findViewById(R.id.Message); snap.setOnClickListener(新View.OnClickListener(){        @覆盖
        公共无效的onClick(视图v){            // TODO自动生成方法存根
             字符串imageFilePath = Environment.getExternalStorageDirectory()。
             getAbsolutePath()+/picture.jpg;
             文件镜像文件=新的文件(imageFilePath);
             乌里imageFileUri = Uri.fromFile(镜像文件); //路径转换为乌里             它的意图=新意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
             it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,imageFileUri);
             startActivityForResult(它,CAMERA_RESULT);
            }
        });
}

在code接收的意图是:

 情况下CAMERA_RESULT:
        如果(结果code == Activity.RESULT_OK){                    //从额外的意图
                    捆绑额外= data.getExtras();
                    //从外返回的图像
                    BMP位=(位图)extras.get(数据);                    imgView =(ImageView的)findViewById(R.id.ImageView);
                    imgView.setImageBitmap(BMP);
              }
        打破;

我还收到以下例外情况发生应用程序崩溃时:


  

了java.lang.RuntimeException:无法恢复活动{com.NYP.estatemanagement / com.NYP.estatemanagement.MainActivity}了java.lang.RuntimeException:不提供结果ResultInfo {谁= NULL,请求= 100,结果= -1,数据= NULL}到活动{com.NYP.estatemanagement / com.NYP.estatemanagement.MainActivity}:显示java.lang.NullPointerException



解决方案

试试这个正在般的魅力与我

 私人字符串selectedImagePath =;
    最后的私人INT PICK_IMAGE = 1;
    最后的私人INT CAPTURE_IMAGE = 2;公共乌里setImageUri(){
        在DCIM //专卖店形象
        档案文件=新的文件(Environment.getExternalStorageDirectory()+/ DCIM /,形象+新的Date()的getTime()+巴纽);
        乌里imgUri = Uri.fromFile(文件);
        this.imgPath = file.getAbsolutePath();
        返回imgUri;
    }
    公共字符串getImagePath(){
        返回imgPath;
    }btnGallery.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                意向意图=新的Intent();
                intent.setType(图像/ *);
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(意向,),PICK_IMAGE);            }
        });        btnCapture.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                最终意向意图=新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,setImageUri());
                startActivityForResult(意向,CAPTURE_IMAGE);
            }
        });@覆盖
    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        如果(结果code!= Activity.RESULT_CANCELED){
            如果(要求code == PICK_IMAGE){
                selectedImagePath = getAbsolutePath(data.getData());
                imgUser.setImageBitmap(德codeFILE(selectedImagePath));
            }否则如果(要求code == CAPTURE_IMAGE){
                selectedImagePath = getImagePath();
                imgUser.setImageBitmap(德codeFILE(selectedImagePath));
            }其他{
                super.onActivityResult(要求code,结果code,数据);
            }
        }    }
公共位图德codeFILE(字符串路径){
        尝试{
            //德code图像尺寸
            BitmapFactory.Options O =新BitmapFactory.Options();
            o.inJustDe codeBounds = TRUE;
            BitmapFactory.de codeFILE(路径,O);
            //我们希望新的大小缩放到
            最终诠释REQUIRED_SIZE = 70;            //找到正确的比例值。它应该是2的幂。
            int标= 1;
            而(o.outWidth /规模/ 2 - = REQUIRED_SIZE和放大器;&安培; o.outHeight /规模/ 2 - = REQUIRED_SIZE)
                规模* = 2;            //德code。与inSampleSize
            BitmapFactory.Options O2 =新BitmapFactory.Options();
            o2.inSampleSize =规模;
            返回BitmapFactory.de codeFILE(路径,O2);
        }赶上(Throwable的E){
            e.printStackTrace();
        }
        返回null;    }公共字符串getAbsolutePath(URI URI){
        的String [] =投影{MediaColumns.DATA};
        @燮pressWarnings(德precation)
        光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
        如果(指针!= NULL){
            INT与Column_Index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            返回cursor.getString(Column_Index中);
        }其他
            返回null;
    }

I have been trying to pass the image data back to the app after capturing it. However, it always crash while trying to return to the my app.

The Code for starting the intent is:

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

    imgView = (ImageView) findViewById(R.id.ImageView);
    upload = (Button) findViewById(R.id.Upload);
    snap = (Button) findViewById(R.id.Snap);
    select = (Button) findViewById(R.id.Select);
    subject = (EditText) findViewById(R.id.Subject);
    msg = (EditText) findViewById(R.id.Message);snap.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
             String imageFilePath = Environment.getExternalStorageDirectory().
             getAbsolutePath() + "/picture.jpg";  
             File imageFile = new File(imageFilePath); 
             Uri imageFileUri = Uri.fromFile(imageFile); // convert path to Uri

             Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
             it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri); 
             startActivityForResult(it, CAMERA_RESULT);
            }
        });
}

The code to receive the intent is:

 case CAMERA_RESULT:
        if (resultCode == Activity.RESULT_OK) {

                    // Get Extra from the intent 
                    Bundle extras = data.getExtras(); 
                    // Get the returned image from extra 
                    Bitmap bmp = (Bitmap) extras.get("data"); 

                    imgView = (ImageView) findViewById(R.id.ImageView); 
                    imgView.setImageBitmap(bmp); 
              }
        break;

I also receive the following exceptions when the app crash occurred:

java.lang.RuntimeException: Unable to resume activity {com.NYP.estatemanagement/com.NYP.estatemanagement.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.NYP.estatemanagement/com.NYP.estatemanagement.MainActivity}: java.lang.NullPointerException

解决方案

Try this is working like charm with me

private String selectedImagePath = "";
    final private int PICK_IMAGE = 1;
    final private int CAPTURE_IMAGE = 2;

public Uri setImageUri() {
        // Store image in dcim
        File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
        Uri imgUri = Uri.fromFile(file);
        this.imgPath = file.getAbsolutePath();
        return imgUri;
    }


    public String getImagePath() {
        return imgPath;
    }

btnGallery.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);

            }
        });

        btnCapture.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
                startActivityForResult(intent, CAPTURE_IMAGE);
            }
        });

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
            if (requestCode == PICK_IMAGE) {
                selectedImagePath = getAbsolutePath(data.getData());
                imgUser.setImageBitmap(decodeFile(selectedImagePath));
            } else if (requestCode == CAPTURE_IMAGE) {
                selectedImagePath = getImagePath();
                imgUser.setImageBitmap(decodeFile(selectedImagePath));
            } else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }

    }


public Bitmap decodeFile(String path) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            // The new size we want to scale to
            final int REQUIRED_SIZE = 70;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeFile(path, o2);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;

    }

public String getAbsolutePath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }

这篇关于无法通过图像数据传回Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆