无法从图库上传图片

时间:2011-11-14 08:53:21

标签: android image-uploading

我想让用户从图库中选择一张图片,或者在点击图片视图时使用相机拍照。我在网上关注了几个教程,这是我的尝试:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class UploadImageActivity extends Activity {
    private final int CAMERA_PICTURE = 1;
    private final int GALLERY_PICTURE = 2;
    private ImageView userPictureImageView;
    private Intent pictureActionIntent = null;

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

        userPictureImageView = (ImageView) findViewById(R.id.image_view);
        userPictureImageView.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                startDialog();
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GALLERY_PICTURE) {
            Uri uri = data.getData();
            if (uri != null) {
                // User had pick an image.
                Cursor cursor = getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
                cursor.moveToFirst();
                // Link to the image
                final String imageFilePath = cursor.getString(0);
                File photos = new File(imageFilePath);
                Bitmap b = decodeFile(photos);
                b = Bitmap.createScaledBitmap(b, 150, 150, true);
                userPictureImageView.setImageBitmap(b);
                cursor.close();
            }
            else {
                Toast toast = Toast.makeText(this, "No Image is selected.", Toast.LENGTH_LONG);
                toast.show();
            }
        }
        else if (requestCode == CAMERA_PICTURE) {
            if (data.getExtras() != null) {
                // here is the image from camera
                Bitmap bitmap = (Bitmap) data.getExtras().get("data");
                userPictureImageView.setImageBitmap(bitmap);
            }
        }
    }

    private Bitmap decodeFile(File f) {
        try {
            // decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE = 70;
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale++;
            }

            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        }
        catch (FileNotFoundException e) {
        }
        return null;
    }

    private void startDialog() {
        AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
        myAlertDialog.setTitle("Upload Pictures Option");
        myAlertDialog.setMessage("How do you want to set your picture?");

        myAlertDialog.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                pictureActionIntent = new Intent(Intent.ACTION_GET_CONTENT, null);
                pictureActionIntent.setType("image/*");
                pictureActionIntent.putExtra("return-data", true);
                startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
            }
        });

        myAlertDialog.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                pictureActionIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(pictureActionIntent, CAMERA_PICTURE);
            }
        });
        myAlertDialog.show();
    }
}

相机工作正常,但画廊根本不起作用。由于某些原因,它一直在崩溃,有人能让我失明吗?感谢。

修改

错误记录

11-14 01:09:30.566: E/AndroidRuntime(24630): FATAL EXCEPTION: main
11-14 01:09:30.566: E/AndroidRuntime(24630): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.csun.spotr/com.csun.spotr.UploadImageActivity}: java.lang.NullPointerException
11-14 01:09:30.566: E/AndroidRuntime(24630):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
11-14 01:09:30.566: E/AndroidRuntime(24630):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
11-14 01:09:30.566: E/AndroidRuntime(24630):    at android.app.ActivityThread.access$2000(ActivityThread.java:117)
11-14 01:09:30.566: E/AndroidRuntime(24630):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
11-14 01:09:30.566: E/AndroidRuntime(24630):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 01:09:30.566: E/AndroidRuntime(24630):    at android.os.Looper.loop(Looper.java:130)
11-14 01:09:30.566: E/AndroidRuntime(24630):    at android.app.ActivityThread.main(ActivityThread.java:3683)
11-14 01:09:30.566: E/AndroidRuntime(24630):    at java.lang.reflect.Method.invokeNative(Native Method)
11-14 01:09:30.566: E/AndroidRuntime(24630):    at java.lang.reflect.Method.invoke(Method.java:507)
11-14 01:09:30.566: E/AndroidRuntime(24630):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-14 01:09:30.566: E/AndroidRuntime(24630):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-14 01:09:30.566: E/AndroidRuntime(24630):    at dalvik.system.NativeStart.main(Native Method)
11-14 01:09:30.566: E/AndroidRuntime(24630): Caused by: java.lang.NullPointerException
11-14 01:09:30.566: E/AndroidRuntime(24630):    at com.csun.spotr.UploadImageActivity.onActivityResult(UploadImageActivity.java:42)
11-14 01:09:30.566: E/AndroidRuntime(24630):    at android.app.Activity.dispatchActivityResult(Activity.java:3908)
11-14 01:09:30.566: E/AndroidRuntime(24630):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
11-14 01:09:30.566: E/AndroidRuntime(24630):    ... 11 more

刚才意识到如果我把这条线拿出来: super.onActivityResult(requestCode, resultCode, data); 然后它不再崩溃,但是在图像视图中没有弹出来自Gallery的图像。有什么想法吗?

4 个答案:

答案 0 :(得分:1)

        private static final int SELECT_PICTURE = 1;
        private String selectedImagePath, filemanagerstring;
        private static File myFile = null;

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);

        startActivityForResult(Intent.createChooser(intent,
                "Select Picture"), SELECT_PICTURE);

onActivityresult ::

           if (data != null) {
            Uri selectedImageUri = data.getData();
            filemanagerstring = selectedImageUri.getPath();
            selectedImagePath = getPath(selectedImageUri);

            if (selectedImagePath != null)
                myFile = new File(selectedImagePath);
            else if (filemanagerstring != null)
                myFile = new File(filemanagerstring);

            if (myFile != null) {

                Bitmap bmp_fromGallery = decodeImageFile(selectedImagePath);


                System.out.println("Bitmap is :: " + myPhoto_bitmap);
                //SET BITMAP TO IMAGEVIEW
            } else {
                Toast.makeText(getApplicationContext(),
                        myFile.getName() + "is null", Toast.LENGTH_LONG)
                        .show();
            }
        } else {
            Toast.makeText(SelectPhoto.this, "Please Select Image!!!",
                    Toast.LENGTH_LONG).show();
        }

答案 1 :(得分:0)

你不能在eclipse中发送任何值为null的东西。您将始终获得nullPointerException。

引起: 显示java.lang.NullPointerException
在com.csun.spotr.UploadImageActivity.onActivityResult(UploadImageActivity.java:42)

第一个原因是引发错误的代码的行号

答案 2 :(得分:0)

 CircleImageView profile;
Bitmap bmp;
SharedPreferences sp;
public static final int PERMISSION_REQUEST_CAMERA = 1;
private static final int PICK_FROM_GALLERY = 1;

onCreate()方法:

 sp=getSharedPreferences("profilePicture",MODE_PRIVATE);
    profile=(CircleImageView)findViewById(R.id.profile);
    if(!sp.getString("dp","").equals("")){
        byte[] decodedString = Base64.decode(sp.getString("dp", ""), Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        profile.setImageBitmap(decodedByte);
    }

 profile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openGallery();
        }
    });

openGallery()

 private void openGallery() {
    if (ActivityCompat.checkSelfPermission(ProfileActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(ProfileActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PICK_FROM_GALLERY);
    }
    else {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, 1);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults)
{
    switch (requestCode) {
        case PICK_FROM_GALLERY:
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
            } else {
                Toast.makeText(ProfileActivity.this,"Not working",Toast.LENGTH_LONG).show();
                //do something like displaying a message that he didn`t allow the app to access gallery and you wont be able to let him select from gallery
            }
            break;
    }
}

onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{

    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        try {
            // When an Image is picked
            if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data

                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                // Get the cursor
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String imgDecodableString = cursor.getString(columnIndex);
                cursor.close();
                // Set the Image in ImageView after decoding the String
                bmp = BitmapFactory
                        .decodeFile(imgDecodableString);
                profile.setImageBitmap(bmp);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();

                bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();
                String encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
                sp.edit().putString("dp", encodedImage).commit();
            } else {
                Toast.makeText(this, "You haven't picked Image",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(ProfileActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
        }

    }else {
        Toast.makeText(ProfileActivity.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
    }
}

答案 3 :(得分:0)

选择图像的方法

private void showImageChooser(){

    Intent intent= new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"Select Your Profile Picture"),CHOOSE_IMAGE)
}     

“ Uri = uriprofileImage”(在AppCompactAcivity内部全局创建此变量)

OnActivityResult方法

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == CHOOSE_IMAGE && resultCode == RESULT_OK && data!=null && data.getData() != null) {

        uriprofileImage =  data.getData();

        try {
            //getting the image
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),uriprofileImage);

            //displaying the image
            imageView.setImageBitmap(bitmap);


        } catch (IOException e) {

            e.printStackTrace();

        }
    }
}