intent.putExtra(MediaStore.EXTRA_OUTPUT,getOutputMediaFileUri(MEDIA_TYPE_IMAGE)); //设置图片文件名 为什么让我的应用程序发生冲突
在主要活动中,此命令
intent.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri(MEDIA_TYPE_IMAGE));
此代码对此代码的影响
startActivityForResult(intent, MEDIA_TYPE_IMAGE);
然后它使我的应用崩溃了
您可以专注于
public void takingPhoto()
public void takingVideo()
功能
MainActivity.java
package com.example.labinclass;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.Activity;
import android.view.Menu;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import static java.security.AccessController.getContext;
public class MainActivity extends AppCompatActivity {
private Uri fileUri;
private Button takePhoto;
private Button takeVideo;
private Button sharePhoto;
private ImageView captureImage;
private Intent intent;
private Bitmap bp;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takePhoto = (Button) findViewById(R.id.takePhoto);
takeVideo = (Button) findViewById(R.id.takeVideo);
sharePhoto = (Button) findViewById(R.id.sharePhoto);
captureImage = (ImageView) findViewById(R.id.captureImage);
if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[] {
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.RECORD_AUDIO
},110);
}
takingPhoto();
takingVideo();
shareStory();
}
public void takingPhoto() {
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri(MEDIA_TYPE_IMAGE)); // set the image file name **Why It's Error**
startActivityForResult(intent, MEDIA_TYPE_IMAGE);
}
});
}
public void takingVideo() {
takeVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri(MEDIA_TYPE_VIDEO)); // set the image file name **Why It's Error**
startActivityForResult(intent, MEDIA_TYPE_VIDEO);
}
});
}
public void shareStory(){
sharePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), bp,null, null);
Uri uri = Uri.parse(bitmapPath);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setPackage("com.instagram.android"); //Instagram App package
startActivity(Intent.createChooser(shareIntent, "Share to"));*/
Intent showInstagram = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
startActivity(showInstagram);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == MEDIA_TYPE_IMAGE) {
if (resultCode == RESULT_OK) {
data.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri(MEDIA_TYPE_IMAGE)); // set the image file name
bp = (Bitmap) data.getExtras().get("data");
captureImage.setScaleType (ImageView.ScaleType.FIT_CENTER);
captureImage.setImageBitmap(bp);
} else if (resultCode == RESULT_CANCELED) {
android.widget.Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
}
}else if (requestCode == MEDIA_TYPE_VIDEO) {
if (resultCode == RESULT_OK) {
//How can I Show Video on Phone Screen ?????????????????????????????????
} else if (resultCode == RESULT_CANCELED) {
android.widget.Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
}
}
}
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "LabInClass");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("LabInClass", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg");
}
else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_"+ timeStamp + ".mp4");
}
else {
return null;
}
return mediaFile;
}
}
并且可以在onActivityResult中在“电话”屏幕上显示我的视频
activity_main.xml
我认为这部分几乎是正确的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:weightSum="20"
tools:context=".MainActivity">
<ImageView
android:id="@+id/captureImage"
android:rotation="90"
android:layout_margin="10dp"
android:layout_weight="13"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/takePhoto"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textColor="@android:color/white"
android:text="Take Photo"
android:background="@android:color/holo_red_dark" />
<Button
android:id="@+id/takeVideo"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textColor="@android:color/white"
android:text="Take Video"
android:background="@android:color/holo_red_dark" />
<Button
android:id="@+id/sharePhoto"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textColor="@android:color/white"
android:text="Intent Instagram"
android:background="@android:color/holo_red_dark" />
</LinearLayout>
AndroidMainfest.xml
我认为这部分和上部分几乎是正确的
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.labinclass">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的堆栈跟踪
2019-03-28 14:02:52.502 8069-8069/com.example.labinclass E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.labinclass, PID: 8069
android.os.FileUriExposedException: file:///storage/emulated/0/Pictures/LabInClass/IMG_20190328_140252.jpg exposed beyond app through ClipData.Item.getUri()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1958)
at android.net.Uri.checkFileUriExposed(Uri.java:2359)
at android.content.ClipData.prepareToLeaveProcess(ClipData.java:941)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9756)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9741)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1611)
at android.app.Activity.startActivityForResult(Activity.java:4479)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:68)
at android.app.Activity.startActivityForResult(Activity.java:4437)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:751)
at com.example.labinclass.MainActivity$1.onClick(MainActivity.java:74)
at android.view.View.performClick(View.java:6274)
at android.view.View$PerformClick.run(View.java:24859)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)