0

我是通过意图开始画廊,否则它会给在网格视图中显示画廊带来问题。但我想要我从图库中选择的图像的实际 sd 卡路径,并按意图打开。

这是代码..

    Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
    photoPickerIntent.setType("image/jpg");
    photoPickerIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Pictures/image.jpg"));
    startActivityForResult(photoPickerIntent, 1);



protected void onActivityResult(int requestCode, int resultCode, Intent intent)      {
   super.onActivityResult(requestCode, resultCode, intent);

          if (resultCode == RESULT_CANCELED) {
           showToast(this,"Activity cancelled");
           return;
          }
           else if(resultCode == RESULT_OK) {


             System.out.println("requestCode--"+requestCode);
             System.out.println("resultCode--"+resultCode);
             System.out.println("data--"+intent.getData());
             Uri uri = intent.getData();
             String data = uri.getPath();
             System.out.println("uri.getPath()--"+uri.getPath());
             System.out.println("type--"+intent.getType());
             System.out.println("path--"+Environment.getExternalStorageState());


                return;

              }

             switch (requestCode) {
                 case CAMERA_ACTIVITY: 
                     Bundle b = intent.getExtras();
                     Bitmap bm = (Bitmap) b.get("data");
              //       mImageView.setImageBitmap(bm); // Display image in the View

                     // large image?
                       if (b.containsKey(MediaStore.EXTRA_OUTPUT)) { // large image?
                       Log.i(TAG, "This is a large image");
                          showToast(this,"Large image");
                       // Should have to do nothing for big images -- should already saved in MediaStore ... but
                           MediaStore.Images.Media.insertImage(getContentResolver(), bm, null, null);
                           } 
                            else {
                               Log.i(TAG, "This is a small image");
                               showToast(this,"Small image");
                              MediaStore.Images.Media.insertImage(getContentResolver(), bm, null, null);
                           }
                          break;
                         }
                  }
                }

我正在从意图对象获取所有数据。但我想要图像的 sd 卡路径,我选择将该图像上传到服务器..

我怎么得到那个?

4

2 回答 2

1

你可能想要这样的东西:

Uri uri = (Uri) intent.getExtras().get("android.intent.extra.STREAM"); 

if ( intent.getAction().equals( Intent.ACTION_SEND ) )
{
    if ( uri.getScheme().equals("content"))
    { 
        Cursor cursor = getContentResolver().query( uri, null, null, null, null );
        cursor.moveToFirst();
        filePath = cursor.getString(cursor.getColumnIndexOrThrow(Images.Media.DATA));
    }
}
于 2012-01-15T15:53:24.157 回答
0

本准则可能对您有所帮助

此代码将弹出一个对话框,其中包含相机和图库以及设备 SD 卡以获取图像路径。

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import android.app.Activity;
import android.app.Dialog;
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.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

import com.fanosedu.R;


public class FileAttachPopUpUtils extends Activity {

    private View rootView;
    private Dialog popup;
    private Button Assig_PopUp_DeviceBtn;
    private Button Assig_PopUp_GalaryBtn;
    private Button Assi_PopUp_CameraBtn;
    private Button Assig_PopUp_CancelPopupBtn;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.assignment_popupfile);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        /*
         * getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
         * WindowManager.LayoutParams.FLAG_FULLSCREEN);
         */
        popup = new Dialog(this);
        // popup.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // popup.getWindow().setBackgroundDrawable(new
        // ColorDrawable(android.graphics.Color.TRANSPARENT));
        popup.setContentView(R.layout.assignment_popupfile);

        popup.setCanceledOnTouchOutside(false);

        popup.show();

        Assig_PopUp_DeviceBtn = (Button) popup
                .findViewById(R.id.Assignment_PopUp_DeviceBtn);

        Assig_PopUp_GalaryBtn = (Button) popup
                .findViewById(R.id.Assignment_PopUp_GalaryBtn);
        Assi_PopUp_CameraBtn = (Button) popup
                .findViewById(R.id.Assignment_PopUp_CameraBtn);

        Assig_PopUp_CancelPopupBtn = (Button) popup
                .findViewById(R.id.Assignment_PopUp_CancelPopupBtn);

        Assig_PopUp_DeviceBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                /*
                 * Toast.makeText(FileAttachPopUpUtils.this,
                 * "Device File In-Progress", Toast.LENGTH_SHORT) .show();
                 */
                Intent intent = new Intent(FileAttachPopUpUtils.this,
                        FileExplore.class);
                startActivity(intent);
                popup.dismiss();
                finish();

            }
        });

        Assig_PopUp_GalaryBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 2);

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

                // ///////////////////////////////////////////////////////////////

                Intent intent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 1);
            }
        });

        Assig_PopUp_CancelPopupBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                popup.dismiss();
                FileAttachPopUpUtils.this.finish();
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == this.RESULT_OK) {
            if (requestCode == 1) {

                /*
                 * Intent intent = new Intent( Intent.ACTION_PICK,
                 * android.provider
                 * .MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                 * startActivityForResult(intent, 2);
                 */

                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                // mImage.setImageBitmap(thumbnail);
                // 3
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                // 4
                String name;
                int n = 100000;
                int rand;
                rand = new Random().nextInt(n);
                name = "Image-" + rand + ".jpg";
                // File fileimage = new File(path, name);
                File file = new File(Environment.getExternalStorageDirectory()
                        + File.separator + name);

                System.out.println("FILE PATH=======>>>>>>>>>>"
                        + file.toString());
                try {
                    file.createNewFile();
                    FileOutputStream fo = new FileOutputStream(file);
                    // 5
                    fo.write(bytes.toByteArray());
                    fo.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                Assignments.mulPathArry.add(file.toString());
                // Here You will get the full path

                FileAttachPopUpUtils.this.finish();

            } else if (requestCode == 2) {

                Uri selectedImage = data.getData();
                String[] filePath = { MediaStore.Images.Media.DATA };
                Cursor c = this.getContentResolver().query(selectedImage,
                        filePath, null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePath[0]);
                String picturePath = c.getString(columnIndex);
                c.close();

                Assignments.mulPathArry.add(picturePath);
                // Here you will get Path

                popup.dismiss();
                FileAttachPopUpUtils.this.finish();
            }
        }

        // ///////////////////////////////////////////////////////////////////////

    }

    @Override
    public void onBackPressed() {
        return;
    }

}
于 2013-12-05T10:06:35.207 回答