无法在android中获取选择器文件的路径

时间:2015-01-08 12:24:55

标签: java android

我在simpleWindow类中使用此代码打开视频选择器。

ImageView btnselect = (ImageView) view.findViewById(R.id.imageView2);
btnselect.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setType("video/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        StandOutExampleActivity.a.startActivity(Intent.createChooser(intent, "Play Video"));    
    }
});

这个simpleWindow不会扩展活动类。那么如何在这个类中使用'onActivityResult'呢?

这是我的onActivityResult方法,但是当我在课堂上使用它时,我收到了错误。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.e("requestCode",""+requestCode);
    if (requestCode == 1) {           
        Uri selectedImageUri = data.getData();
        imagepath= getRealPathFromURI(selectedImageUri);
        Toast.makeText(a, imagepath, Toast.LENGTH_LONG).show();
    }
}

那么我怎样才能在课堂上选择文件路径?

2 个答案:

答案 0 :(得分:1)

public class MainScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_screen);



    SelectVedio = (ImageButton) findViewById(R.id.btnSelectVedio);
    SelectVedio.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            Intent mediaChooser = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            // comma-separated MIME types
            mediaChooser.setType("video/*");
            startActivityForResult(mediaChooser, RESULT_LOAD_VEDIO);
        }
    });



}


    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        // Uri targetUri = data.getData();

        String path;

        path = getRealPathFromURI(data.getData());



}
}

你的问题对我来说是不可理解的。但是如果你遇到问题那么请记住2分

  1. 你可以在activity类中调用OnActivityResult方法 2.如果你想在活动类的旁边调用OnActivityResult,那么你应该将活动类的上下文传递给非活动类..

答案 1 :(得分:0)

这可能对您有所帮助,因为它与您的疑问有些相关:

How to get path of a captured image in android

final private int PICK_IMAGE = 1;
final private int CAPTURE_IMAGE = 2;
private String imgPath;

btnGallery.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
    }
});

btnCapture.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
        startActivityForResult(intent, CAPTURE_IMAGE);
    }
});


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

public String getImagePath() {
    return imgPath;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_CANCELED) {
        if (requestCode == PICK_IMAGE) {
            selectedImagePath = getAbsolutePath(data.getData());
            imgUser.setImageBitmap(decodeFile(selectedImagePath));
        } else if (requestCode == CAPTURE_IMAGE) {
            selectedImagePath = getImagePath();
            imgUser.setImageBitmap(decodeFile(selectedImagePath));
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

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

public Bitmap decodeFile(String path) {
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, o);
        // The new size we want to scale to
        final int REQUIRED_SIZE = 70;
        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
            scale *= 2;

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeFile(path, o2);
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return null;
}

他在这里选择一张图片。您可以为文件实现相同的图像。 或者:Getting the video path of capture video using default camera in android