我创建了一个用于下载文档的下载管理器,但它始终显示吐司download failed
。即使我尚未完成下载的祝酒词,它仍然存在并且我下载的文档已成功下载。
这是我的代码:
class ShareDokumenActivity : AppCompatActivity() {
var tempDatas: ArrayList<ListWa>? = null
var list: ArrayList<Long> = ArrayList()
private var mgr: DownloadManager? = null
private val MY_PERMISSION_REQUEST_WRITE_STORAGE = 100
var filename = ""
var fileUrl = ""
var ext = ""
var type =""
var newNumbCode =""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_list_wa)
titleSearch.setText("Share file to...")
titleSearch.setTextColor(Color.WHITE)
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)
val savedUser = Gson().fromJson<LoginUser>(this@ShareDokumenActivity
.getSharedPreferences(LoginActivity.MY_LOGIN_PREF, Context.MODE_PRIVATE)
.getString(LoginActivity.MY_LOGIN_PREF_KEY, ""), LoginUser::class.java)
getListWa(savedUser.salesmanId)
chooseShareTo()
}
fun getListWa(id : String){
API.getListWa(id).enqueue(object : Callback<ArrayList<ListWa>> {
override fun onResponse(call: Call<ArrayList<ListWa>>, response: Response<ArrayList<ListWa>>) {
if (response.code() == 200) {
tempDatas = response.body();
rvListWa.setHasFixedSize(true)
rvListWa.layoutManager = LinearLayoutManager(this@ShareDokumenActivity)
rvListWa.adapter = ListWaAdapter(tempDatas)
}else{
Toast.makeText(this@ShareDokumenActivity, "Error", Toast.LENGTH_LONG).show()
}
}
override fun onFailure(call: Call<ArrayList<ListWa>>, throwable: Throwable) {
Toast.makeText(this@ShareDokumenActivity, "Error", Toast.LENGTH_LONG).show()
}
})
}
fun chooseShareTo(){
rvListWa!!.addOnItemTouchListener(RecyclerItemClickListener(this@ShareDokumenActivity,
RecyclerItemClickListener.OnItemClickListener { view, position ->
val numbCode = tempDatas!![position].custHpWa
if (numbCode.substring(0,2).equals("08")){
newNumbCode = "62"+numbCode.substring(1)
fileUrl = baseURLPicasso + intent.getStringExtra(SalesProgramDocViewersActivity.DOC_URL_INTENT)
val mime = MimeTypeMap.getSingleton()
ext = fileUrl.substring(fileUrl.lastIndexOf(".") + 1)
type = mime.getMimeTypeFromExtension(ext)
filename = intent.getStringExtra(SalesProgramDocViewersActivity.DOC_URL_INTENT).substring(44)
val downloadUri = Uri.parse(fileUrl)
if (ContextCompat.checkSelfPermission(this@ShareDokumenActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this@ShareDokumenActivity,
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA),
MY_PERMISSION_REQUEST_WRITE_STORAGE)
} else {
mgr?.enqueue(DownloadManager.Request(downloadUri)
.setAllowedOverRoaming(false)
.setTitle("Downloading..")
.setDescription("Download " + filename)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename))
}
}else if (numbCode.substring(0,2).equals("+6")){
newNumbCode = numbCode.substring(1)
Toast.makeText(this@ShareDokumenActivity, "$newNumbCode", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(this@ShareDokumenActivity, "Kode nomor salah", Toast.LENGTH_SHORT).show()
}
}))
}
fun sendFile(filename: String, extension : String, phoneNumb : String) {
if (ContextCompat.checkSelfPermission(this@ShareDokumenActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this@ShareDokumenActivity,
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA),
MY_PERMISSION_REQUEST_WRITE_STORAGE)
} else {
val dir = File(Environment.getExternalStorageDirectory().toString() + "/Download/" + filename)
val intent = Intent(Intent.ACTION_SEND)
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
val fileUri = FileProvider.getUriForFile(this@ShareDokumenActivity, this@ShareDokumenActivity.applicationContext.packageName + ".provider", dir)
intent.setDataAndType(fileUri, extension)
intent.putExtra(Intent.EXTRA_STREAM, fileUri)
intent.putExtra("jid", PhoneNumberUtils.stripSeparators(phoneNumb) + "@s.whatsapp.net")
} else {
intent.setDataAndType(Uri.fromFile(dir), extension)
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(dir))
intent.putExtra("jid", PhoneNumberUtils.stripSeparators(phoneNumb) + "@s.whatsapp.net")
}
try {
this@ShareDokumenActivity.startActivity(intent)
// Toast.makeText(this@ShareDokumenActivity, "Download Success", Toast.LENGTH_LONG).show()
} catch (ex: Exception) {
ex.printStackTrace()
Toast.makeText(this@ShareDokumenActivity, "Can't sharing", Toast.LENGTH_LONG).show()
}
}
}
override fun onResume() {
super.onResume()
mgr = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager?
registerReceiver(onComplete, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
}
var onComplete : BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(ctxt: Context, intent: Intent) {
sendFile(filename, type, newNumbCode)
}
}