█ 【安卓学习之常见问题】文件分享–文件不存在
█ 系列文章目录
提示:这里是收集了和文件分享有关的文章
█ 文章目录
█ 读前说明
- 本文通过学习别人写demo,学习一些课件,参考一些博客,’学习相关知识,如果涉及侵权请告知
- 本文只简单罗列相关的代码实现过程
- 涉及到的逻辑以及说明也只是简单介绍,主要当做笔记,了解过程而已
█ 问题
-
文件不存在
-
之前都可以正常发送
█ 使用过程
- 文件路径的传输过程(数据有点出入,因为刚开始测试是这样,后面测试又没有这个问题)
依赖库 | 获取值 | 方法 | 结果 |
---|---|---|---|
- | File | new File() | /storage/emulated/0/ Android/data/com.bx.share/files/share/xxx.mp3 |
androidx.core: core:1.5.0 | Uri | FileProvider.getUriForFile(this, authority, file) | content://com.bx.share.fileprovider/external_files/ Android/data/com.bx.share/files/share/xxx.mp3 |
androidx.core: core:1.6.0 | Uri | FileProvider.getUriForFile(this, authority, file) | content://com.bx.share.fileprovider/files_root/ Android/data/com.bx.share/files/share/xxx.mp3 |
- 代码实现:
File shareFile = new File(mDirName, mFileName);// /storage/emulated/0/Android/data/com.bx.share/files/
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// content://com.bx.share.fileprovider/external_files/Android/data/com.bx.share/files/share/xxx.mp3
uri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", shareFile);
} else {
uri = Uri.fromFile(shareFile);
}
Intent intent = getChooserIntent(uri);
Intent pickIntent = new Intent();
pickIntent.setAction(Intent.ACTION_PICK_ACTIVITY);
pickIntent.putExtra(Intent.EXTRA_TITLE, "4.分享文件 Pick Activity");
pickIntent.putExtra(Intent.EXTRA_INTENT, intent);
startActivityForResult(pickIntent, RESULT_SHARE);
public Intent getChooserIntent(Uri uri) {
Intent intent = new Intent(Intent.ACTION_SEND);// 共享文件,部分app需要手机联网
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);// 传递权限
intent.putExtra(Intent.EXTRA_TEXT, "分享测试123");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("application/*");// 二进制文件 "application/octet-stream" "*/*"
return intent;
}
█ 正常使用
- 代码:
public class SupportYesNoDialog extends DialogFragment {
protected final static String EXTRA_DIALOG_TAG = "extra_dialog_tag";
protected final static String EXTRA_TITLE = "title";
protected final static String EXTRA_MESSAGE = "message";
protected final static String EXTRA_POSITIVE_LABEL = "positive_label";
protected final static String EXTRA_NEGATIVE_LABEL = "negative_label";
protected Listener mListener;
public interface Listener {
void onDialogYes(String dialogTag);
void onDialogNo(String dialogTag);
}
public static SupportYesNoDialog newInstance(Context context, String dialogTag, String title, String msg) {
if (dialogTag == null)
throw new IllegalArgumentException("The dialog tag must not be null!");
SupportYesNoDialog f = new SupportYesNoDialog();
Bundle b = new Bundle();
b.putString(EXTRA_DIALOG_TAG, dialogTag);
b.putString(EXTRA_TITLE, title);
b.putString(EXTRA_MESSAGE, msg);
b.putString(EXTRA_POSITIVE_LABEL, context.getString(android.R.string.yes));
b.putString(EXTRA_NEGATIVE_LABEL, context.getString(android.R.string.no));
f.setArguments(b);
return f;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return buildDialog(savedInstanceState).create();
}
protected AlertDialog.Builder buildDialog(Bundle savedInstanceState){
final Bundle arguments = getArguments();
final String dialogTag = arguments.getString(EXTRA_DIALOG_TAG);
AlertDialog.Builder b = new AlertDialog.Builder(getActivity())
.setTitle(arguments.getString(EXTRA_TITLE))
.setView(generateContentView(savedInstanceState))
.setPositiveButton(arguments.getString(EXTRA_POSITIVE_LABEL), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (mListener != null)
mListener.onDialogYes(dialogTag);
}
})
.setNegativeButton(arguments.getString(EXTRA_NEGATIVE_LABEL), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (mListener != null)
mListener.onDialogNo(dialogTag);
}
});
return b;
}
public boolean isShown() {
return getDialog() != null && getDialog().isShowing();
}
@Override
public void show(@NonNull FragmentManager manager, @Nullable String tag) {
if (isShown()) {
return;
}
if (isAdded() && getDialog() != null) {
getDialog().show();
return;
}
super.show(manager, tag);
}
}
- 偶尔会出现:IllegalStateException
private SupportYesNoDialog dialog;
private void getConfirmation(String title, String msg) {
if (dialog== null) {
dialog = mSupportYesNoDialog.newInstance(this, "fragment_Tag", title, msg);
}
dialog.show(getSupportFragmentManager(), "fragment_Tag");
}
#236002 java.lang.IllegalStateException
Fragment already added: SupportYesNoDialog{865d6b8} (8f2fb666-b09e-47cf-a60f-9fde9f9559b6) fragment_Tag}
androidx.fragment.app.FragmentStore.addFragment(FragmentStore.java:67)
█ IllegalStateException:Fragment already added
- 修改后会发现,调用两次才会弹框一次,
- 实际上,虽然我点击了外部,弹框消失,但是没有执行 onDismiss(),等下次 show()的时候
private SupportYesNoDialog dialog;
private void getConfirmation(String title, String msg) {
if (dialog== null) {
dialog= mSupportYesNoDialog.newInstance(this, "fragment_Tag", title, msg);
}
if (dialog.isAdded()) {
getSupportFragmentManager().beginTransaction().remove(dialog).commit();
}
dialog.show(getSupportFragmentManager(), "fragment_Tag");
}
- 修改后正常
private SupportYesNoDialog dialog;
private void getConfirmation(String title, String msg) {
if (dialog!= null) {
try {
//在每个add事务前增加一个remove事务,防止连续的add
manager.beginTransaction().remove(dialog).commit();
} catch (Exception e) {
//同一实例使用不同的tag会异常,这里捕获一下
e.printStackTrace();
}
}
dialog = mSupportYesNoDialog.newInstance(this, "fragment_Tag", title, msg);
dialog.show(getSupportFragmentManager(), "fragment_Tag");
}
█ 其他
█ 相关资料
提示:这里是参考的相关文章
- Android SVG to VectorDrawable >>>>svg转Vector
- 2016-12-23 Android Vector详解_yw1688的专栏-CSDN博客_android vector
█ 免责声明
博主分享的所有文章内容,部分参考网上教程,引用大神高论,部分亲身实践,记下笔录,内容可能存在诸多不实之处,还望海涵,本内容仅供学习研究使用,切勿用于商业用途,若您是部分内容的作者,不喜欢此内容被分享出来,可联系博主说明相关情况通知删除,感谢您的理解与支持! |
---|
转载请注明出处:
https://blog.csdn.net/ljb568838953/article/details/110383353