无法在android中将图像从相机设置为imageview

时间:2015-01-11 18:10:00

标签: java android

我想做什么:

我正在尝试在Android中显示从相机到imageview捕获的图像

发生了什么:

一旦我捕获图像并设置它图像没有在图像视图中设置而不是片段被破坏


点击按钮,我正在呼叫selectImage()


private void selectImage(){

//final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };

final String[] options = getActivity().getResources().getStringArray(R.array.image_pick_list);

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getActivity().getResources().getString(R.string.str_add_photo));
builder.setItems(options, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int item) {
        if (options[item].equals(getActivity().getResources().getStringArray(R.array.image_pick_list)[0]))
        {
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");

            intent.putExtra("crop", "true");
            intent.putExtra("outputX", 150);
            intent.putExtra("outputY", 150);
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            intent.putExtra("scale", true);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            intent.putExtra("outputFormat",
                    Bitmap.CompressFormat.JPEG.toString());

            //intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            startActivityForResult(intent, 1);
        }
        else if (options[item].equals(getActivity().getResources().getStringArray(R.array.image_pick_list)[1]))
        {
            Intent intent = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, 2);

        }
        else if (options[item].equals(getActivity().getResources().getStringArray(R.array.image_pick_list)[2])) {
            dialog.dismiss();
        }
    }
});
builder.show();

}


@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //----------------------------------SCREEN NAME-------------------------//
        ((ActionBarActivity) getActivity()).getSupportActionBar().setTitle(getString(R.string.screenname_register));

        if (resultCode == getActivity().RESULT_OK) {
            if (requestCode == 1) {
                //FILE IS FROM CAMERA-----SingleIMAGE
                File f = new File(Environment.getExternalStorageDirectory().toString());
                for (File temp : f.listFiles()) {
                    if (temp.getName().equals("temp.jpg")) {
                        f = temp;
                        break;
                    }
                }
                try {
                    Bitmap bitmap;
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                    bitmap=CommonFunctions.CompressImage(f);
                    if(flgProfileOrBannerPicClkd.equalsIgnoreCase("profile")){
                        profilePicImgId.setImageBitmap(bitmap);
                        isProfilePicAdded=true;
                        profileImgId=System.currentTimeMillis() / 1000L;
                        profilePicImgId.setTag(profileImgId+".jpg");
                    }else if(flgProfileOrBannerPicClkd.equalsIgnoreCase("banner")){
                        portfolioPicImgId.setImageBitmap(bitmap);
                        isPortfolioPicAdded=true;
                        portfolioImgId=System.currentTimeMillis() / 1000L;
                        portfolioPicImgId.setTag(portfolioImgId+".jpg");
                    }else{
                        ((ImageView)addressImageView.findViewWithTag("addrImgTag")).setBackgroundResource(0);
                        Picasso.with(getActivity()).load(f).resize(100, 100).into(addressImageView);
                    }

                    String path = android.os.Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "cameraimage" + File.separator + unixTime+".jpg";

                    f.delete();
                    OutputStream outFile = null;
                    File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                    try {
                        outFile = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                        outFile.flush(); ///////////////////////////////////////////// ----- CURSOR NOT CLOSED --- CHECK THIS IF APP CRASHES IN FUTURE
                        outFile.close();///////////////////////////////////////////// ----- CURSOR NOT CLOSED --- CHECK THIS IF APP CRASHES IN FUTURE
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

...... other code

日志:

01-11 23:38:38.175: W/System.err(4531): java.lang.NullPointerException
01-11 23:38:38.179: W/System.err(4531):     at com.windhyaworks.fragments.FrgRegisterAsServiceProvider.onActivityResult(FrgRegisterAsServiceProvider.java:457)
01-11 23:38:38.179: W/System.err(4531):     at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:163)
01-11 23:38:38.179: W/System.err(4531):     at android.app.Activity.dispatchActivityResult(Activity.java:5446)
01-11 23:38:38.179: W/System.err(4531):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3428)
01-11 23:38:38.179: W/System.err(4531):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2788)
01-11 23:38:38.179: W/System.err(4531):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2845)
01-11 23:38:38.179: W/System.err(4531):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2262)
01-11 23:38:38.179: W/System.err(4531):     at android.app.ActivityThread.access$800(ActivityThread.java:139)
01-11 23:38:38.179: W/System.err(4531):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
01-11 23:38:38.179: W/System.err(4531):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-11 23:38:38.179: W/System.err(4531):     at android.os.Looper.loop(Looper.java:136)
01-11 23:38:38.179: W/System.err(4531):     at android.app.ActivityThread.main(ActivityThread.java:5086)
01-11 23:38:38.179: W/System.err(4531):     at java.lang.reflect.Method.invokeNative(Native Method)
01-11 23:38:38.180: W/System.err(4531):     at java.lang.reflect.Method.invoke(Method.java:515)
01-11 23:38:38.180: W/System.err(4531):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-11 23:38:38.180: W/System.err(4531):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-11 23:38:38.180: W/System.err(4531):     at dalvik.system.NativeStart.main(Native Method)
01-11 23:38:38.256: I/Adreno-EGL(4531): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LNX.LA.3.5.1_RB1.04.04.02.048.018_msm8610_LNX.LA.3.5.1_RB1__release_AU ()
01-11 23:38:38.256: I/Adreno-EGL(4531): OpenGL ES Shader Compiler Version: E031.24.00.08
01-11 23:38:38.256: I/Adreno-EGL(4531): Build Date: 03/07/14 Fri
01-11 23:38:38.256: I/Adreno-EGL(4531): Local Branch: 
01-11 23:38:38.256: I/Adreno-EGL(4531): Remote Branch: quic/LNX.LA.3.5.1_RB1.1
01-11 23:38:38.256: I/Adreno-EGL(4531): Local Patches: NONE
01-11 23:38:38.256: I/Adreno-EGL(4531): Reconstruct Branch: AU_LINUX_ANDROID_LNX.LA.3.5.1_RB1.04.04.02.048.018 + f2fd134 +  NOTHING
01-11 23:38:38.291: D/OpenGLRenderer(4531): Enabling debug mode 0
01-11 23:38:39.540: I/System.out(4531): {"message":"Not a Vendor","status":"False","isVendor":"False"}
01-11 23:38:39.607: I/dalvikvm(4531): Could not find method android.view.ViewGroup.isTransitionGroup, referenced from method android.support.v4.app.FragmentTransitionCompat21.captureTransitioningViews
01-11 23:38:39.607: W/dalvikvm(4531): VFY: unable to resolve virtual method 11818: Landroid/view/ViewGroup;.isTransitionGroup ()Z
01-11 23:38:39.607: D/dalvikvm(4531): VFY: replacing opcode 0x6e at 0x000d
01-11 23:38:39.608: I/dalvikvm(4531): Could not find method android.view.View.getTransitionName, referenced from method android.support.v4.app.FragmentTransitionCompat21.findNamedViews
01-11 23:38:39.608: W/dalvikvm(4531): VFY: unable to resolve virtual method 11647: Landroid/view/View;.getTransitionName ()Ljava/lang/String;
01-11 23:38:39.608: D/dalvikvm(4531): VFY: replacing opcode 0x6e at 0x0006
01-11 23:38:39.609: I/dalvikvm(4531): Could not find method android.view.View.getTransitionName, referenced from method android.support.v4.app.FragmentTransitionCompat21.getTransitionName
01-11 23:38:39.609: W/dalvikvm(4531): VFY: unable to resolve virtual method 11647: Landroid/view/View;.getTransitionName ()Ljava/lang/String;
01-11 23:38:39.609: D/dalvikvm(4531): VFY: replacing opcode 0x6e at 0x0000
01-11 23:38:39.610: W/dalvikvm(4531): Unable to resolve superclass of Landroid/support/v4/app/FragmentTransitionCompat21$1; (1376)
01-11 23:38:39.610: W/dalvikvm(4531): Link of class 'Landroid/support/v4/app/FragmentTransitionCompat21$1;' failed
01-11 23:38:39.610: E/dalvikvm(4531): Could not find class 'android.support.v4.app.FragmentTransitionCompat21$1', referenced from method android.support.v4.app.FragmentTransitionCompat21.setEpicenter
01-11 23:38:39.610: W/dalvikvm(4531): VFY: unable to resolve new-instance 370 (Landroid/support/v4/app/FragmentTransitionCompat21$1;) in Landroid/support/v4/app/FragmentTransitionCompat21;
01-11 23:38:39.610: D/dalvikvm(4531): VFY: replacing opcode 0x22 at 0x0007
01-11 23:38:39.611: W/dalvikvm(4531): Unable to resolve superclass of Landroid/support/v4/app/FragmentTransitionCompat21$3; (1376)
01-11 23:38:39.611: W/dalvikvm(4531): Link of class 'Landroid/support/v4/app/FragmentTransitionCompat21$3;' failed
01-11 23:38:39.612: E/dalvikvm(4531): Could not find class 'android.support.v4.app.FragmentTransitionCompat21$3', referenced from method android.support.v4.app.FragmentTransitionCompat21.setSharedElementEpicenter
01-11 23:38:39.612: W/dalvikvm(4531): VFY: unable to resolve new-instance 372 (Landroid/support/v4/app/FragmentTransitionCompat21$3;) in Landroid/support/v4/app/FragmentTransitionCompat21;
01-11 23:38:39.612: D/dalvikvm(4531): VFY: replacing opcode 0x22 at 0x0002
01-11 23:38:39.612: W/dalvikvm(4531): Unable to resolve superclass of Landroid/support/v4/app/FragmentTransitionCompat21$1; (1376)
01-11 23:38:39.613: W/dalvikvm(4531): Link of class 'Landroid/support/v4/app/FragmentTransitionCompat21$1;' failed
01-11 23:38:39.613: D/dalvikvm(4531): DexOpt: unable to opt direct call 0x082f at 0x09 in Landroid/support/v4/app/FragmentTransitionCompat21;.setEpicenter
01-11 23:38:39.613: W/dalvikvm(4531): Unable to resolve superclass of Landroid/support/v4/app/FragmentTransitionCompat21$3; (1376)
01-11 23:38:39.613: W/dalvikvm(4531): Link of class 'Landroid/support/v4/app/FragmentTransitionCompat21$3;' failed
01-11 23:38:39.614: D/dalvikvm(4531): DexOpt: unable to opt direct call 0x0833 at 0x04 in Landroid/support/v4/app/FragmentTransitionCompat21;.setSharedElementEpicenter

0 个答案:

没有答案