我正在使用Picasso从画廊中获取图像。我在SO上围绕同一主题关注了一些问题,但似乎没有解决我的问题。我从onActivityResult获取了我的文件路径,并尝试将“file:”+ .... +“.jpg”添加到文件并将其设置为ImageView。但它似乎没有适应它。
public class PictureDialog extends DialogFragment {
private ImageView imageView;
private RelativeLayout relativeLayout;
private Button saveMemory;
private Bitmap bitmap, bMapScaled;
private FinishedMemorySaving fMS;
public interface FinishedMemorySaving {
void showMemory(Memory memory);
}
public PictureDialog(){
}
public static PictureDialog newInstance(Memory memory){
PictureDialog pictureDialog = new PictureDialog();
Bundle args = new Bundle();
args.putString("title", memory.getTitleMem());
args.putString("desc", memory.getDescMem());
args.putDouble("lat", memory.getLocationMem().latitude);
args.putDouble("lng", memory.getLocationMem().longitude);
args.putString("date", memory.getFormatedDate());
pictureDialog.setArguments(args);
return pictureDialog;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a window without the title
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.picture_dialog, container);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final String title = getArguments().getString("title");
final String desc = getArguments().getString("desc");
final double lat = getArguments().getDouble("lat");
final double lng = getArguments().getDouble("lng");
imageView = (ImageView)view.findViewById(R.id.memPic);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 1);
}
});
saveMemory = (Button)view.findViewById(R.id.saveMemory);
saveMemory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Memory memory = new Memory();
memory.setTitleMem(title);
memory.setDescMem(desc);
memory.setLocationMem(new LatLng(lat, lng));
memory.setFormatedDate(new Date());
memory.setImageMem(bitmap);
try {
saveToCloud(memory);
} catch (ParseException e) {
e.printStackTrace();
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == Activity.RESULT_OK && data != null){
String path = data.getData().getPath();
File file = new File("file:" + String.valueOf(path) + ".jpg");
Log.i("FILE ERROR", "file:" + path + ".jpg");
setPicImage(file);
//imageView.setImageBitmap(bMapScaled);
}
}
public void setPicImage(File file){
if(file == null){
Log.i("FILE ERROR", "No file " + file.getPath());
} else {
// WHERE I WANT TO SET THE IMAGE TO FROM PICASSO
Picasso.with(getContext()).load(file).centerInside().fit().into(imageView);
}
}
.........
}
当我在添加“file:”等时打印出文件路径时,我得到了这个:
文件:/external/images/media/6854.jpg
希望能找到一些可以帮助我将其设置为ImageView的东西。感谢。