1

I am opening video chooser by using this code in simpleWindow class.

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

This simpleWindow is not extending activity class. So how can I use 'onActivityResult' in this class?

Here is my onActivityResult method, But when I am using this in my class, I am getting error.

@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();
    }
}

So how I can get selected file path in my class?

sam
  • 2,780
  • 1
  • 17
  • 30
  • This may help you as it is somewhat related to your doubt: https://stackoverflow.com/questions/20856601/how-to-get-path-of-a-captured-image-in-android – Rishabh Srivastava Jan 08 '15 at 12:29
  • No, You didn't understand what i am saying. My class is extending another class 'sayX', So i cant extend it with 'Activity', So i am opening file chooser by using 'StandOutExampleActivity' class, File chooser is opened, And i am able to select video, But if i am implementing 'onActivityResult' method in 'StandOutExampleActivity' It's not showing 'Toast', And i can't use this method in my class. So how where i use this method to show toast. – Preeti Gupta Jan 08 '15 at 12:35
  • The cleanest practical solution would be to implement that method in your Activity class with a call through to this other one. – Chris Stratton Jan 08 '15 at 13:16
  • @ChrisStratton Can you please explain what you are trying to say – Preeti Gupta Jan 09 '15 at 05:01
  • Looks like similar problem, But no solution is there http://www.xiandg.com/3574052/codep2/android-switching-between-two-activities – Preeti Gupta Jan 09 '15 at 05:02

2 Answers2

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());



}
}

Your Question is not clearable for me.but if u will face problem then remember 2 points

  1. u can call OnActivityResult method with in the activity class 2.if you want to call OnActivityResult out side the activity class then u should pass the context of your activity class to your non activity class..
  • No asim, its not helping me, I know this all-ready, Please look at this http://www.xiandg.com/3574052/codep2/android-switching-between-two-activities same problem i am facing. – Preeti Gupta Jan 09 '15 at 05:03
  • dear you should extend activity class where you call onActivityResult.Or pass the context of activity class to your class. – asim mahmood Khan Jan 09 '15 at 07:19
  • dear check these links[link1](http://stackoverflow.com/a/16964145/4419437)[link2](http://stackoverflow.com/a/21793981/4419437).will help you – asim mahmood Khan Jan 09 '15 at 08:13
  • Please look at this http://s21.postimg.org/3vrizkksn/image.png , When i click on '+', Then file chooser is open, But after choosing file i am not able to get capture path if i add 'onActivityResult' in my main activity class. – Preeti Gupta Jan 09 '15 at 11:47
0

This may help you as it is somewhat related to your doubt:

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

Here he is selecting an image.You can implement same for a file. Or this: Getting the video path of capture video using default camera in android

Community
  • 1
  • 1
Rishabh Srivastava
  • 3,683
  • 2
  • 30
  • 58