161 lines
6.0 KiB
Java
161 lines
6.0 KiB
Java
package cn.shangyu.gdxzExpert.activity;
|
|
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.media.MediaMetadataRetriever;
|
|
import android.net.Uri;
|
|
import android.os.Build;
|
|
import android.view.View;
|
|
import android.widget.Toast;
|
|
|
|
import com.github.barteksc.pdfviewer.PDFView;
|
|
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
|
|
import com.gyf.immersionbar.ImmersionBar;
|
|
|
|
import java.io.File;
|
|
|
|
import cn.shangyu.gdxzExpert.R;
|
|
import cn.shangyu.gdxzExpert.base.BaseActivity;
|
|
import cn.shangyu.gdxzExpert.utils.ToastUtil;
|
|
import cn.shangyu.gdxzExpert.utils.UIUtils;
|
|
import cn.shangyu.gdxzExpert.widget.LoadingPage;
|
|
|
|
/**
|
|
* Created by wangchengxin on 2018/11/8.
|
|
*/
|
|
|
|
public class PdfViewActivity extends BaseActivity {
|
|
private String path;
|
|
private File file;
|
|
@Override
|
|
public void setTitle() {
|
|
String title = getIntent().getExtras().getString("title");
|
|
top_title.setText(title);
|
|
top_right.setVisibility(View.VISIBLE);
|
|
top_right.setImageResource(R.drawable.share_icon);
|
|
top_right.setOnClickListener(this);
|
|
|
|
}
|
|
@Override
|
|
public View onCreateSuccessedView() {
|
|
ImmersionBar.with(this).keyboardEnable(true).statusBarDarkFont(true).titleBar(mToolbar).init();
|
|
View view = UIUtils.inflate(R.layout.activity_pdfview);
|
|
|
|
path = getIntent().getExtras().getString("path");
|
|
// Log.d("gdxzfile",path);
|
|
file = new File(path);
|
|
PDFView pdfView = view.findViewById(R.id.pdfView);
|
|
pdfView.fromFile(file).defaultPage(0).onPageChange(new OnPageChangeListener() {
|
|
@Override
|
|
public void onPageChanged(int page, int pageCount) {
|
|
ToastUtil.showMessage((page+1) + " / " + pageCount);
|
|
}
|
|
}) .load();
|
|
// initView(view);
|
|
return view;
|
|
}
|
|
|
|
private void initView(View view) {
|
|
|
|
PDFView pdfView = view.findViewById(R.id.pdfView);
|
|
pdfView.fromFile(file).defaultPage(0).onPageChange(new OnPageChangeListener() {
|
|
@Override
|
|
public void onPageChanged(int page, int pageCount) {
|
|
ToastUtil.showMessage((page+1) + " / " + pageCount);
|
|
}
|
|
}) .load();
|
|
|
|
}
|
|
|
|
@Override
|
|
public LoadingPage.ResultState onLoad() {
|
|
return LoadingPage.ResultState.STATE_SUCCESSED;
|
|
}
|
|
|
|
@Override
|
|
protected void processClick(View v) {
|
|
switch (v.getId()){
|
|
|
|
case R.id.top_right:
|
|
shareFile(PdfViewActivity.this,file);
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
public void shareFile(Context context, File file) {
|
|
if (null != file && file.exists()) {
|
|
Intent share = new Intent(Intent.ACTION_SEND);
|
|
// Intent share = new Intent(Intent.ACTION_VIEW);
|
|
// share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
// String s2 = null;
|
|
// try {
|
|
// s2 = new String(path.getBytes(),"utf-8");
|
|
// } catch (UnsupportedEncodingException e) {
|
|
// e.printStackTrace();
|
|
// }
|
|
// if(Build.VERSION.SDK_INT >= 24){
|
|
// // 适配android7.0 ,不能直接访问原路径
|
|
// // 需要对intent 授权
|
|
// share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
// share.setDataAndType(FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider",new File(s2)),"application/octet-stream");
|
|
// }
|
|
// else{
|
|
// share.setDataAndType(Uri.fromFile(new File(s2)), "application/octet-stream");
|
|
// }
|
|
Uri uri ;
|
|
|
|
// Uri uri = Uri.fromFile(file);
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
|
|
{
|
|
uri = androidx.core.content.FileProvider
|
|
.getUriForFile(this, "cn.shangyu.gdxzExpert.fileprovider", file);
|
|
}
|
|
else
|
|
{
|
|
uri = Uri.fromFile(file);
|
|
}
|
|
share.putExtra(Intent.EXTRA_STREAM,uri );
|
|
share.setType("image/jpeg");
|
|
share.setType("application/octet-stream");
|
|
|
|
// Log.d("gdxzfile","uri "+uri);
|
|
|
|
// share.setPackage("com.tencent.mm");
|
|
// context.startActivity(share);
|
|
// share.setType(getMimeType(file.getAbsolutePath()));//此处可发送多种文件
|
|
// share.putExtra("Kdescription", "pdf文档");//微信朋友圈专用
|
|
// share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
// share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
context.startActivity(Intent.createChooser(share, "分享文件"));
|
|
// Intent intent=new Intent(Intent.ACTION_SEND);
|
|
// intent.setType(getMimeType(file.getAbsolutePath()));//此处可发送多种文件
|
|
// intent.putExtra(Intent.EXTRA_SUBJECT, "Share");
|
|
// intent.putExtra(Intent.EXTRA_TEXT, "I have successfully share my message through my app");
|
|
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
// context.startActivity(Intent.createChooser(intent, "fdasfsadfsa"));
|
|
} else {
|
|
Toast.makeText(context, "分享文件不存在", Toast.LENGTH_SHORT).show();
|
|
}
|
|
}
|
|
// 根据文件后缀名获得对应的MIME类型。
|
|
private static String getMimeType(String filePath) {
|
|
|
|
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
|
|
String mime = "*/*";
|
|
if (filePath != null) {
|
|
try {
|
|
mmr.setDataSource(filePath);
|
|
mime = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE);
|
|
} catch (IllegalStateException e) {
|
|
return mime;
|
|
} catch (IllegalArgumentException e) {
|
|
return mime;
|
|
} catch (RuntimeException e) {
|
|
return mime;
|
|
}
|
|
}
|
|
return mime;
|
|
}
|
|
}
|