These code examples were ranked by Codota’s semantic indexing as the best open source examples for Intent setDataAndType method.
You can now enable Codota on your own code to easily search and navigate your Java codebase.
117: Intent viewIntent = new Intent(Intent.ACTION_VIEW);
-
120: viewIntent.setDataAndType(uri, mime);
121: return mContext.getPackageManager().resolveActivity(
Full Snippet Info
124: Intent intent = new Intent(Intent.ACTION_VIEW);
125: intent.setDataAndType(path, mimetype);
Full Snippet Info
43: Intent i=new Intent(getActivity(), Downloader.class);
-
45: i.setDataAndType(Uri.parse("http://commonsware.com/Android/excerpt.pdf"),
-
48: getActivity().startService(i);
Full Snippet Info
34: Intent i=new Intent(Intent.ACTION_VIEW);
-
36: i.setDataAndType(Uri.fromFile(output), "image/jpeg");
Full Snippet Info
97: Intent intent = new Intent(Intent.ACTION_VIEW);
-
99: intent.setDataAndType(Uri.fromFile(new File(fileName)),
-
101: intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
102: intent.setClassName("com.android.packageinstaller",
Full Snippet Info
81: Intent intent = new Intent(Intent.ACTION_VIEW);
-
84: intent.setDataAndType(ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id), type);
Full Snippet Info
68: Intent intent = new Intent(Intent.ACTION_VIEW);
69: intent.setDataAndType(Uri.parse(url), mimetype);
70: ResolveInfo info = activity.getPackageManager().resolveActivity(intent,
Full Snippet Info
74: Intent launchIntent = new Intent(Intent.ACTION_VIEW);
75: launchIntent.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(id));
76: launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Full Snippet Info
57: Intent intent = new Intent(Intent.ACTION_VIEW);
58: intent.setDataAndType(Uri.fromFile(napk),
Full Snippet Info
81: Intent intent = new Intent("com.android.camera.action.CROP", myIntent.getData());
-
83: intent.setDataAndType(myIntent.getData(), myIntent.getStringExtra("mimeType"));
-
85: intent.putExtra("crop", "true");
86: intent.putExtra("aspectX", 1);
87: intent.putExtra("aspectY", 1);
88: intent.putExtra("outputX", 96);
89: intent.putExtra("outputY", 96);
90: intent.putExtra("return-data", true);
Full Snippet Info
42: Uri selectedVideo = data.getData();
43: Intent intent = new Intent(Intent.ACTION_VIEW);
44: intent.setDataAndType(selectedVideo, "video/*");
45: startActivity(Intent.createChooser(intent,
46: "Complete action using"));
You need to Override onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PHOTO) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedVideo = data.getData();
// do something. play video using uri
}
}
}
Edit:
Change
ImageButton vb = (ImageButton) findViewById(R.id.video);
vb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(UI.this, "Video Testimonial", Toast.LENGTH_LONG).show();
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("video/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PHOTO) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedVideoLocation = data.getData();
// do something
}
}
}
});
TO
ImageButton vb = (ImageButton) findViewById(R.id.video);
vb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(UI.this, "Video Testimonial", Toast.LENGTH_LONG).show();
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("video/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PHOTO) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedVideoLocation = data.getData();
// do something
}
}
}
Edit 2:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedVideo = data.getData();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType( selectedVideo,"video/*");
startActivity(intent);
}
}
}
Edit 3:
Full Code
public class MainActivity extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 5000;
private static final int SELECT_PHOTO = 100;
private static final int SELECT_VIDEO = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fg);
ImageButton ab = (ImageButton) findViewById(R.id.imageButton1);
ab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Audio Testimonial", Toast.LENGTH_LONG)
.show();
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 1);
}
});
ImageButton vb = (ImageButton) findViewById(R.id.imageButton2);
vb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Video Testimonial", Toast.LENGTH_LONG)
.show();
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("video/*");
startActivityForResult(photoPickerIntent, SELECT_VIDEO);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_VIDEO) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedVideo = data.getData();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedVideo, "video/*");
startActivity(Intent.createChooser(intent,
"Complete action using"));
}
}
// ImageButton wb = (ImageButton) findViewById(R.id.imageButton3);
// wb.setOnClickListener(new View.OnClickListener() {
// public void onClick(View v) {
// Toast.makeText(MainActivity.this, "Written Testimonial",
// Toast.LENGTH_LONG).show();
// Intent intent = new Intent(MainActivity.this, Written.class);
// startActivity(intent);
// }
// });
ImageButton pb = (ImageButton) findViewById(R.id.imageButton4);
pb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Before and After Photos",
Toast.LENGTH_LONG).show();
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
}
}
fg.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageButton2"
android:layout_alignParentTop="true"
android:layout_marginTop="24dp"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageButton2"
android:layout_below="@+id/imageButton2"
android:layout_marginTop="52dp"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="80dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
Snaps
6: Uri selectedVideo = data.getData();
7: Intent intent = new Intent(Intent.ACTION_VIEW);
8: intent.setDataAndType( selectedVideo,"video/*");
9: startActivity(intent);
10:
You need to Override onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PHOTO) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedVideo = data.getData();
// do something. play video using uri
}
}
}
Edit:
Change
ImageButton vb = (ImageButton) findViewById(R.id.video);
vb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(UI.this, "Video Testimonial", Toast.LENGTH_LONG).show();
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("video/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PHOTO) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedVideoLocation = data.getData();
// do something
}
}
}
});
TO
ImageButton vb = (ImageButton) findViewById(R.id.video);
vb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(UI.this, "Video Testimonial", Toast.LENGTH_LONG).show();
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("video/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PHOTO) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedVideoLocation = data.getData();
// do something
}
}
}
Edit 2:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedVideo = data.getData();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType( selectedVideo,"video/*");
startActivity(intent);
}
}
}
Edit 3:
Full Code
public class MainActivity extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 5000;
private static final int SELECT_PHOTO = 100;
private static final int SELECT_VIDEO = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fg);
ImageButton ab = (ImageButton) findViewById(R.id.imageButton1);
ab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Audio Testimonial", Toast.LENGTH_LONG)
.show();
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 1);
}
});
ImageButton vb = (ImageButton) findViewById(R.id.imageButton2);
vb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Video Testimonial", Toast.LENGTH_LONG)
.show();
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("video/*");
startActivityForResult(photoPickerIntent, SELECT_VIDEO);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_VIDEO) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedVideo = data.getData();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedVideo, "video/*");
startActivity(Intent.createChooser(intent,
"Complete action using"));
}
}
// ImageButton wb = (ImageButton) findViewById(R.id.imageButton3);
// wb.setOnClickListener(new View.OnClickListener() {
// public void onClick(View v) {
// Toast.makeText(MainActivity.this, "Written Testimonial",
// Toast.LENGTH_LONG).show();
// Intent intent = new Intent(MainActivity.this, Written.class);
// startActivity(intent);
// }
// });
ImageButton pb = (ImageButton) findViewById(R.id.imageButton4);
pb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Before and After Photos",
Toast.LENGTH_LONG).show();
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
}
}
fg.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageButton2"
android:layout_alignParentTop="true"
android:layout_marginTop="24dp"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageButton2"
android:layout_below="@+id/imageButton2"
android:layout_marginTop="52dp"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="80dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
Snaps
7: Uri selectedVideo = data.getData();
8: Intent intent = new Intent(Intent.ACTION_VIEW);
9: intent.setDataAndType(selectedVideo, "video/*");
10: startActivity(intent);
11: } else if (requestCode == SELECT_PHOTO) {
-
14: Intent intent = new Intent();
15: intent.setAction(Intent.ACTION_VIEW);
16: intent.setDataAndType(selectedImage, "image/*");
17: startActivity(intent);
18: }
data
is null
. You need to check before that line if data
is null
. Since you are pressing the Back button, you aren't sending an Intent
back with setResult()
like would normally be done.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if (resultCode == Activity.RESULT_OK) {
if (data != null)
{
if (requestCode == SELECT_VIDEO) {
Uri selectedVideo = data.getData();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedVideo, "video/*");
startActivity(intent);
} else if (requestCode == SELECT_PHOTO) {
Uri selectedImage = data.getData();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(selectedImage, "image/*");
startActivity(intent);
}
}
4: Intent cropIntent = new Intent("com.android.camera.action.CROP");
5: // indicate image type and Uri
6: cropIntent.setDataAndType(picUri, "image/*");
7: // set crop properties
8: cropIntent.putExtra("crop", "true");
I think what you are looking for is cropping image.
you can achieve that with default android Crop functionality:
private void performCrop(Uri picUri) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
Constant declare:
final int PIC_CROP = 1;
override onActivity result method in your activity, writ following code:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PIC_CROP) {
if (data != null) {
// get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedBitmap = extras.getParcelable("data");
imgView.setImageBitmap(selectedBitmap);
}
}
}
1: Intent promptInstall = new Intent(Intent.ACTION_VIEW)
2: .setDataAndType(Uri.parse("file:///path/to/your.apk"),
3: "application/vnd.android.package-archive");
4: startActivity(promptInstall);
Your best bet would be to set an intent with type application/vnd.android.package-archive and then start installer activity:
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.parse("file:///path/to/your.apk"),
"application/vnd.android.package-archive");
startActivity(promptInstall);
Check out Install Application programmatically on Android
Also, note, that for security reasons, you cannot install an apk 'silently' or automatically without user agreement. Therefore, the best you can do is like above.
联系客服