Android读取“_File”里面的文件,例如图片,哪位大侠请给一份例程,我是新手
这是我写的,哪位大侠指点一下
package com.qqhr.leadcloud;
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.util.Log;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import cn.leancloud.AVFile;
import cn.leancloud.AVObject;
import cn.leancloud.AVQuery;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
public class MainActivity extends AppCompatActivity {
private AVFile file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
file = new AVFile(
"ic_launcher_shi.png",
"http://lc-x5xUS9c8.cn-n1.lcfile.com/sWahVRAXIB505GL3wlJQAff2JV0zGIQ0/ic_launcher_shi.png",
new HashMap<String, Object>()
);
AVObject todo = new AVObject("Todo");
todo.put("title", "数据库");
todo.add("fuJia", file);
todo.save();
//程序到这就停止了
AVQuery<AVObject> query = new AVQuery<>("Todo");
query.whereEqualTo("title", "数据库");
query.whereExists("fuJia");
query.include("fuJia");
query.findInBackground().subscribe(new Observer<List<AVObject>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(List<AVObject> avObjects) {
for (AVObject avObject : avObjects)
{
AVFile avFile = avObject.getAVFile("fuJia");
avFile.getDataStreamInBackground().subscribe(new Observer<InputStream>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(InputStream inputStream) {
try {
byte[] buffer = new byte[102400];
int read = inputStream.read(buffer);
Log.i("tongzhi", "file data length:" + read);
inputStream.close();
Log.i("tongzhi", "成功");
} catch (IOException e) {
e.printStackTrace();
Log.i("tongzhi", "失败");
}
}
@Override
public void onError(Throwable e) {
Log.i("tongzhi", "没有获取数据:" + e.getMessage());
}
@Override
public void onComplete() {
}
});
}
}
@Override
public void onError(Throwable e) {
Log.i("tongzhi", "查询失败");
}
@Override
public void onComplete() {
}
});
}
}