java下载另存为_java - 如何下载文件并将其另存为JPEG格式 - SO中文参考 - www.soinside.com...

这就是你的课程应该是什么样子。

public class YourActivity extends Activity {

private WebView webView; // make sure to init your webview

private DownloadManager downloadManager;

@Override

public void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// your any other onCreate() code...

downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

registerReceiver(onDownloadComplete,

new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

}

@Override

public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {

super.onCreateContextMenu(contextMenu, view, contextMenuInfo);

final WebView.HitTestResult webViewHitTestResult = webView.getHitTestResult();

if (isHitResultAnImage(webViewHitTestResult)) {

contextMenu.setHeaderTitle("Download Image");

contextMenu.add(0, 1, 0, "Save - Download Image")

.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

@Override

public boolean onMenuItemClick(MenuItem menuItem) {

return handleMenuItemClick(menuItem, webViewHitTestResult.getExtra());

}

});

}

}

@Override

protected void onDestroy() {

super.onDestroy();

unregisterReceiver(onDownloadComplete);

}

private boolean isHitResultAnImage(WebView.HitTestResult hitTestResult) {

return hitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||

hitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE;

}

private boolean handleMenuItemClick(MenuItem menuItem, String imageDownloadUrl) {

if(URLUtil.isValidUrl(imageDownloadUrl)){

downloadImage(imageDownloadUrl);

Toast.makeText(this,"Image Downloaded Successfully.",Toast.LENGTH_LONG).show();

return false;

}

Toast.makeText(this,"Sorry.. Something Went Wrong.",Toast.LENGTH_LONG).show();

return false;

}

private void downloadImage(String imageUrl) {

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(imageUrl));

request.allowScanningByMediaScanner();

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

downloadManager.enqueue(request);

}

public String getFilePathFromUri(Uri uri) {

String filePath = null;

if ("content".equals(uri.getScheme())) {

String[] filePathColumn = { MediaStore.MediaColumns.DATA };

ContentResolver contentResolver = getContentResolver();

Cursor cursor = contentResolver.query(uri, filePathColumn, null,

null, null);

cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

filePath = cursor.getString(columnIndex);

cursor.close();

} else if ("file".equals(uri.getScheme())) {

filePath = new File(uri.getPath()).getAbsolutePath();

}

return filePath;

}

private void saveAsJpeg(Bitmap bitmapImage) {

ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());

// path to /data/data/yourapp/app_data/imageDir

File directory = contextWrapper.getDir("imageDir", Context.MODE_PRIVATE);

// Create imageDir

File mypath = new File(directory,"imageName.jpg");

FileOutputStream fileOutputStream = null;

try {

fileOutputStream = new FileOutputStream(mypath);

// Use the compress method on the BitMap object to write image to the OutputStream

bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (fileOutputStream != null) {

fileOutputStream.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

BroadcastReceiver onDownloadComplete = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {

long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);

DownloadManager.Query query = new DownloadManager.Query();

query.setFilterById(downloadId);

Cursor cursor = downloadManager.query(query);

if (cursor.moveToFirst()) {

int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);

if (DownloadManager.STATUS_SUCCESSFUL == cursor.getInt(columnIndex)) {

String uriString = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

File file = new File(getFilePathFromUri(Uri.parse(uriString)));

try {

Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(file));

saveAsJpeg(bitmap);

} catch (FileNotFoundException e) {

// cant save

}

} else {

// downloadFailed, show toast or something..

}

}

}

};

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值