通过alertdialog从图库或相机上传图片

时间:2014-08-12 10:53:11

标签: android

这是我的程序,我认为alertdialogue没问题。 但是在我选择了我的图库中的照片后,图片没有改变。 它仍然是我在ImageView中设置的原始照片。 有些人可以帮助我知道问题出在哪里了吗?

public class MainActivity extends Activity{
ImageView img_logo;<br>
protected static final int CAMERA_REQUEST = 0;
protected static final int GALLERY_PICTURE = 1;
private Intent pictureActionIntent = null;
Bitmap bitmap;

Uri selectedImageUri;
String  selectedPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    img_logo= (ImageView) findViewById(R.id.homepic);
    img_logo.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startDialog();
        }

    });
}
 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_REQUEST);

                }
            });
    myAlertDialog.show();
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {



    if (resultCode == RESULT_OK) {
     if(data.getData() != null){
       selectedImageUri = data.getData();
     }else{
      Log.d("selectedPath1 : ","Came here its null !");
      Toast.makeText(getApplicationContext(), "failed to get Image!", 500).show();
     }

     if (requestCode == 100 && resultCode == RESULT_OK) {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            selectedPath = getPath(selectedImageUri);
            img_logo.setImageURI(selectedImageUri);
            Log.d("selectedPath1 : " ,selectedPath);

        } 

        if (requestCode == 10)

        {

           selectedPath = getPath(selectedImageUri);
           img_logo.setImageURI(selectedImageUri);
           Log.d("selectedPath1 : " ,selectedPath);

        }

    }
    Toast toast = Toast.makeText(MainActivity.this, "hiiiii", Toast.LENGTH_LONG);
    toast.show();

}
public String getPath(Uri uri) {

    String[] projection = { MediaStore.Images.Media.DATA };

    Cursor cursor = managedQuery(uri, projection, null, null, null);

    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

    cursor.moveToFirst();

    return cursor.getString(column_index);

} 
}

1 个答案:

答案 0 :(得分:1)

请尝试这种方式,希望这有助于您解决问题。

public class MainActivity extends Activity {

    ImageView img_logo;
    protected static final int CAMERA_REQUEST = 0;
    protected static final int GALLERY_PICTURE = 1;

    private String imgPath;
    private String selectedPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img_logo = (ImageView) findViewById(R.id.homepic);
        img_logo.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startDialog();
            }

        });
    }

    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) {
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent, ""), GALLERY_PICTURE);
                    }
                }
        );

        myAlertDialog.setNegativeButton("Camera",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface arg0, int arg1) {
                        final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
                        startActivityForResult(intent, CAMERA_REQUEST);

                    }
                }
        );
        myAlertDialog.show();
    }

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

    public String getImagePath() {
        return imgPath;
    }

    public String getAbsolutePath(Uri uri) {
        if (Build.VERSION.SDK_INT >= 19) {
            String id = uri.getLastPathSegment().split(":")[1];
            final String[] imageColumns = {MediaStore.Images.Media.DATA};
            final String imageOrderBy = null;
            Uri tempUri = getUri();
            Cursor imageCursor = managedQuery(tempUri, imageColumns,
                    MediaStore.Images.Media._ID + "=" + id, null, imageOrderBy);

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

    }

    private Uri getUri() {
        String state = Environment.getExternalStorageState();
        if (!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED))
            return MediaStore.Images.Media.INTERNAL_CONTENT_URI;

        return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == CAMERA_REQUEST) {
                img_logo.setImageBitmap(BitmapFactory.decodeFile(getImagePath()));
                selectedPath = getImagePath();
            } else if (requestCode == GALLERY_PICTURE) {
                img_logo.setImageBitmap(BitmapFactory.decodeFile(getAbsolutePath(data.getData())));
                selectedPath = getAbsolutePath(data.getData());
            }
            Toast toast = Toast.makeText(MainActivity.this, "hiiiii", Toast.LENGTH_LONG);
            toast.show();
        }

    }

}

Add this  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> in AndroidManifest.xml