Java 我怎样才能保存;PDF";在RecylerView中归档?
我正在为学童创建一个离线应用程序。在这个项目中,一个男生点击一个“RecylerView”上传一本书“Pdf”,并将其保存到外部存储器中。我的问题是,它只能使用互联网查看一本书。如果你关闭互联网,很容易看不到pdf文件 请帮忙 实现'com.github.kk121:文件加载器:1.1' 实现'com.karumi:dexter:5.0.0' 我的主要活动Java 我怎样才能保存;PDF";在RecylerView中归档?,java,android,android-studio,pdf,android-recyclerview,Java,Android,Android Studio,Pdf,Android Recyclerview,我正在为学童创建一个离线应用程序。在这个项目中,一个男生点击一个“RecylerView”上传一本书“Pdf”,并将其保存到外部存储器中。我的问题是,它只能使用互联网查看一本书。如果你关闭互联网,很容易看不到pdf文件 请帮忙 实现'com.github.kk121:文件加载器:1.1' 实现'com.karumi:dexter:5.0.0' 我的主要活动 应用程序将PDF文件下载到本地设备后,需要更新列表中的链接。使用书籍信息创建一些数据库。应用程序将PDF文件下载到本地设备后,需要更新列表中
应用程序将PDF文件下载到本地设备后,需要更新列表中的链接。使用书籍信息创建一些数据库。应用程序将PDF文件下载到本地设备后,需要更新列表中的链接。创建一些包含书籍信息的数据库。我刚在阅读了你的标题后浏览了你的代码,所以你要做的是下载文件并将其存储在正确的位置?为了方便地做到这一点,我推荐okhttp,因为这就是我所使用的。看看这个,看看159票的答案,这就是我所用的。将文件路径保存到本地数据库,并使用一些pdf查看库向用户显示它,就像我建议使用一个界面将arraylist位置从适配器传递到活动,并显示一个警报,如果他们想下载它,或者不只是为了避免任何想要的转换,或者至少将onclick侦听器设置为“可能是下载”按钮而不是整个cardviewok谢谢你的想法,但我如何保存文件?它在第一条注释中
文件下载file=new file(context.getCacheDir(),filename)
是我指的。之后的3行代码将把pdf保存在下载文件
路径中。您可以使用getFilesDir()
而不是context.getCacheDir()
,它将为您提供内部存储,您不需要在其中请求权限,pdf将仅在应用程序中可见。但是,如果您希望它在文件管理器中可见,请获得外部存储权限,并为downloadFile
对象提供一个外部路径。在阅读了您的标题后,我刚刚浏览了您的代码,所以您要做的是下载文件并将其存储到正确的位置?为了方便地做到这一点,我推荐okhttp,因为这就是我所使用的。看看这个,看看159票的答案,这就是我所用的。将文件路径保存到本地数据库,并使用一些pdf查看库向用户显示它,就像我建议使用一个界面将arraylist位置从适配器传递到活动,并显示一个警报,如果他们想下载它,或者不只是为了避免任何想要的转换,或者至少将onclick侦听器设置为“可能是下载”按钮而不是整个cardviewok谢谢你的想法,但我如何保存文件?它在第一条注释中文件下载file=new file(context.getCacheDir(),filename)
是我指的。之后的3行代码将把pdf保存在下载文件
路径中。您可以使用getFilesDir()
而不是context.getCacheDir()
,它将为您提供内部存储,您不需要在其中请求权限,pdf将仅在应用程序中可见。但是,如果希望它在文件管理器中可见,请获得外部存储权限,并为downloadFile
对象提供一个外部路径。我认为不需要数据库,我希望当我单击按钮时,我通过“url”下载了文件+我将设备保存到内存中,…确定,每次打开应用程序时,您都会下载文件,您需要存储本地路径和远程url,我想在16:21分钟内完成,直到同一方法结束,但使用place按钮使用“RecylerView”,我认为不需要数据库,我想当我单击按钮时,我通过“url”下载了文件+我已将设备保存到内存中,…好的,每次打开应用程序时,您都将下载该文件,您需要存储本地路径和远程url我要执行的操作16:21分钟,直到相同方法结束,但使用位置按钮使用“RecylerView”
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.List;
> public class MainActivity extends AppCompatActivity {
List<Product> productList;
//the recyclerview
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_about_card_show);
RelativeLayout relativeLayout = findViewById(R.id.rl);
relativeLayout.startAnimation(animation);
//getting the recyclerview from xml
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//initializing the productlist
productList = new ArrayList<>();
//adding some items to our list
productList.add(
new Product(
1,
"TEst 11111 \n",
60000,
R.drawable.android,
"link1"
));
productList.add(
new Product(
1,
" More types, Methods, Conditionals \n",
60000,
R.drawable.android,
"link2"
));
productList.add(
new Product(
1,
"Loops, Arrays ",
60000,
R.drawable.android,
"lin3"
));
productList.add(
new Product(
1,
"Strings",
60000,
R.drawable.android,
"https://firebasestorage.googleapis.com/v0/b/firepdf-4c1d6.appspot.com/o/2.intro.pdf?alt=media&token=75731b04-c1e7-42c4-b988-e50a8f7e5f6b "
));
//creating recyclerview adapter
ProductAdapter adapter = new ProductAdapter(this, productList);
//setting adapter to recyclerview
recyclerView.setAdapter(adapter);
}
}
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
> public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {
//this context we will use to inflate the layout
private Context mCtx;
//we are storing all the products in a list
private List<Product> productList;
//getting the context and product list with constructor
public ProductAdapter(Context mCtx, List<Product> productList) {
this.mCtx = mCtx;
this.productList = productList;
}
@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflating and returning our view holder
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.layout_products, null);
return new ProductViewHolder(view);
}
@Override
public void onBindViewHolder(ProductViewHolder holder, final int position) {
//getting the product of the specified position
final Product product = productList.get(position);
//binding the data with the viewholder views
holder.textViewTitle.setText(product.getTitle());
holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(product.getImage()));
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(), pdf.class);
i.putExtra("title",productList.get(position).getTitle());
i.putExtra("product",productList.get(position).getTitle());
i.putExtra("link",productList.get(position).getLink());
mCtx.startActivity(i);
}
});
}
@Override
public int getItemCount() {
return productList.size();
}
class ProductViewHolder extends RecyclerView.ViewHolder {
TextView textViewTitle;
ImageView imageView;
CardView cardView;
public ProductViewHolder(View itemView) {
super(itemView);
cardView = itemView.findViewById(R.id.cardview);// card intial
textViewTitle = itemView.findViewById(R.id.textViewTitle);
imageView = itemView.findViewById(R.id.imageView);
}
}
}
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;
import com.github.barteksc.pdfviewer.PDFView;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class pdf extends AppCompatActivity {
String link="",productList="",product="";
PDFView pdfView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf);
product =getIntent().getStringExtra("title");
productList=getIntent().getStringExtra("productList");
link=getIntent().getStringExtra("link");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle(productList);
pdfView=findViewById(R.id.pdfv);
//pdfView.fromAsset(link).load();
if (isConnected()) {
Toast.makeText(getApplicationContext(), "Internet Connected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(pdf.this);
builder.setTitle("NoInternet Connection Alert")
.setMessage("GO to Setting ?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(pdf.this,"Go Back TO HomePage!",Toast.LENGTH_SHORT).show();
}
});
//Creating dialog box
AlertDialog dialog = builder.create();
dialog.show();
}
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
link = getIntent().getStringExtra("link");
}
new pdf.RetrievePDFStream().execute(link);
}
public boolean isConnected() {
boolean connected = false;
try {
ConnectivityManager cm = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nInfo = cm.getActiveNetworkInfo();
connected = nInfo != null && nInfo.isAvailable() && nInfo.isConnected();
return connected;
} catch (Exception e) {
Log.e("Connectivity Exception", e.getMessage());
}
return connected;
}
class RetrievePDFStream extends AsyncTask<String, Void, InputStream> {
ProgressDialog progressDialog;
protected void onPreExecute()
{
progressDialog = new ProgressDialog(pdf.this);
progressDialog.setTitle("getting the book content...");
progressDialog.setMessage("Please wait...");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
@Override
protected InputStream doInBackground(String... strings) {
InputStream inputStream = null;
try {
URL urlx = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) urlx.openConnection();
if (urlConnection.getResponseCode() == 200) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
} catch (IOException e) {
return null;
}
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
pdfView.fromStream(inputStream).load();
progressDialog.dismiss();
}
}
@Override public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == android.R.id.home)//means home default hai kya yesok
{
onBackPressed();
return true;
}
return false;
}
}
public class Product {
private int id;
private String title;
private int image;
private String link;
public Product(int id, String title, double price, int image, String link) {
this.id = id;
this.title = title;
this.image = image;
this.link=link;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public int getImage() {
return image;
}
public String getLink() {
return link;
}