选择的图像未显示在mainActivity中

时间:2015-10-22 06:36:00

标签: java android android-studio sdk android-camera

为什么所选图像未显示在Claims.java中?我错过了什么吗?

mainfest.xml

  <uses-permission android:name="android.permission.CAMERA" />
        <uses-feature android:name="android.hardware.camera" />
        <uses-feature android:name="android.hardware.camera.autofocus" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-sdk
            android:minSdkVersion="7"
            android:targetSdkVersion="16" />

camera_main.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fillViewport="false">


<AbsoluteLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp" >

    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Picture"
        android:id="@+id/btnSelectPhoto"
        android:layout_x="111dp"
        android:layout_y="305dp" />

    <Button
        android:layout_width="121dp"
        android:layout_height="61dp"
        android:text="Submit"
        android:id="@+id/button8"
        android:layout_x="131dp"
        android:layout_y="681dp" />

    <ImageView
        android:layout_width="285dp"
        android:layout_height="285dp"
        android:id="@+id/imageView"
        android:layout_x="43dp"
        android:layout_y="376dp"
        android:contentDescription="i" />

</AbsoluteLayout>

</ScrollView>

Claims.java

公共类声明扩展片段{

ImageView viewImage;
Button b;

private String selectedImagePath;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View claims = inflater.inflate(R.layout.camera_main, container, false);
    b = (Button) claims.findViewById(R.id.btnSelectPhoto);
    viewImage=(ImageView)claims.findViewById(R.id.imageView);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectImage();
        }
    });
    return claims;
}


private void selectImage() {

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

    AlertDialog.Builder builder = new AlertDialog.Builder(Claims.this.getActivity());
    builder.setTitle("Add Photo!");
    builder.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (options[item].equals("Take Photo")) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                String state = Environment.getExternalStorageState();
                if (state.equals(Environment.MEDIA_MOUNTED)) {
                    Toast.makeText(getActivity().getApplicationContext(), " mounted ", Toast.LENGTH_LONG).show();

                }
                startActivityForResult(intent, 1);
            } else if (options[item].equals("Choose from Gallery")) {
                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 2);

            } else if (options[item].equals("Cancel")) {
dialog.dismiss();
            }
        }
    });
    builder.show();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == 1) {
            File f = new File(Environment.getExternalStorageDirectory().toString());
            if(f != null){
                Toast.makeText(getActivity().getApplicationContext(), " not null ", Toast.LENGTH_LONG).show();
            }
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;
                    break;
                }
            }
            try {
                Bitmap bitmap;
                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                        bitmapOptions);

                viewImage.setImageBitmap(bitmap);

                String path = android.os.Environment
                        .getExternalStorageDirectory()
                        + File.separator
                        + "Phoenix" + File.separator + "default";
                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();
                    outFile.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestCode == 2) {

            Uri selectedImage = data.getData();
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor c = getActivity().getContentResolver().query(selectedImage, filePath, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePath[0]);
            String picturePath = c.getString(columnIndex);
            if(picturePath.startsWith("/")) picturePath = picturePath.substring(1);
            c.close();
            Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
            Drawable d = new BitmapDrawable(thumbnail);
            viewImage.setBackground(d);
        }
    }
}

当我选择图像时,imageviewe上没有显示任何内容。但如果我从相机捕捉图像,图像可以显示......为什么?

10-22 14:28:11.128    7451-7451/com.example.project.project E/ViewRootImpl﹕ sendUserActionEvent() mView == null
10-22 14:28:15.228    7451-7451/com.example.project.project E/BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: /https:/lh3.googleusercontent.com/-ZiYOXTFTKqQ/URcxPqvEtTI/AAAAAAAAAJw/GN893VrnxGw/I/PANO_20130210_133321.jpg: open failed: ENOENT (No such file or directory)

这是什么意思?我怎样才能解决这个问题?我怎样才能知道sdk中的android studio版本?请帮帮我....谢谢

3 个答案:

答案 0 :(得分:0)

正如我在你的logcat中看到的,你的路径是错误的。

开头有一个额外的“/”。

您可以尝试:

if(picturePath.startsWith("/")) picturePath = picturePath.substring(1);

答案 1 :(得分:0)

我之前遇到过这种情况,我只是这样做了

else if (requestCode == 2) {
                Uri selectedImage = data.getData();
               viewImage.setImageURI(selectedImage);

它有效!希望它也能帮助你...祝你好运

答案 2 :(得分:0)

嗨,Iam也面临同样的问题,我确实喜欢这个......

String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getActivity().getContentResolver().query(data.getData(),
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String imgDecodableString = cursor.getString(columnIndex);
            cursor.close();

            viewImage.setImageBitmap(BitmapFactory
                    .decodeFile(imgDecodableString));

你只需用上面提到的代码

替换if中的代码if(requestCode == 2)

希望这能解决您的问题