2016-09-06 2 views
1

나는 정말로 내가 뭘 잘못하고 있는지 모른다.피카소가 파일에서 이미지를로드하지 않음

public class ComicFragment extends Fragment 
{ 
    private final static String URL1 = "http://192.168.1.143/jerson/sample_comic.jpg"; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) 
    { 
     View view = inflater.inflate(R.layout.fragment_comic, parent, false); 
     ImageView imageView = (ImageView) view.findViewById(R.id.imageview_comic); 

     Point point = getScreenSize(); 
     new DownloadImageTask(getActivity(), imageView, point.x, point.y).execute(URL1); 

     //Uri uri = Uri.parse("http://192.168.1.143/jerson/sample_comic.jpg"); 
     //simpleDraweeView.setImageURI(uri); 

     return view; 
    } 

    private Point getScreenSize() 
    { 
     Point point = new Point(); 
     WindowManager manager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); 
     Display display = manager.getDefaultDisplay(); 
     display.getSize(point); 

     return point; 
    } 

    private byte [] getBitmapByteArray(Bitmap bitmap) 
    { 
     int bytes = bitmap.getByteCount(); 
     ByteBuffer buffer = ByteBuffer.allocate(bytes); 
     bitmap.copyPixelsToBuffer(buffer); 

     return buffer.array(); 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) 
    { 
     super.onViewCreated(view, savedInstanceState); 
    } 

    private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) 
    { 
     // From https://developer.android.com/training/displaying-bitmaps/load-bitmap.html#read-bitmap 

     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) 
     { 

      final int halfHeight = height/2; 
      final int halfWidth = width/2; 

      while ((halfHeight/inSampleSize) >= reqHeight && (halfWidth/inSampleSize) >= reqWidth) 
      { 
       inSampleSize *= 2; 
      } 
     } 

     return inSampleSize; 
    } 

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> 
    { 
     private Context context; 
     private int viewWidth; 
     private int viewHeight; 
     private ImageView canvas; 

     public DownloadImageTask(Context context, ImageView view, int viewWidth, int viewHeight) 
     { 
      this.context = context; 
      this.viewWidth = viewWidth; 
      this.viewHeight = viewHeight; 
      canvas = view; 
     } 

     @Override 
     protected Bitmap doInBackground(String ... urls) 
     { 
      String url = urls[0]; 
      Bitmap comicBitmap = null; 

      FileOutputStream out = null; 

      File root = Environment.getExternalStorageDirectory(); 
      File directory = new File(root.getAbsolutePath() + "/DCIM/tmpimg/cached/"); 

      directory.mkdirs(); 
      File file = new File(directory, "tmp.png"); 

      try 
      { 
       InputStream forGettingSizeOnly = new BufferedInputStream(new URL(url).openStream()); 

       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inJustDecodeBounds = true; 

       BitmapFactory.decodeStream(forGettingSizeOnly, null, options); 
       int outWidth = options.outWidth; 
       int outHeight = options.outHeight; 
       options.inSampleSize = calculateInSampleSize(options, viewWidth, viewHeight); 
       options.inJustDecodeBounds = false; 

       // Make this not load another image from network the second time... 
       InputStream actualImage = new BufferedInputStream(new URL(url).openStream()); 
       Bitmap decodedImage = BitmapFactory.decodeStream(actualImage); 

       out = new FileOutputStream(file); 
       comicBitmap = Bitmap.createBitmap(decodedImage, 0, 0, outWidth, 400); 
       comicBitmap.compress(Bitmap.CompressFormat.PNG, 100, out); 
       out.close(); 

       Log.i(ComicApplication.TAG, "****File saved at : " + file.getAbsolutePath() + "WxH" + outWidth + " x " + comicBitmap.getHeight()); 
      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
       Log.i(ComicApplication.TAG, "ERROR : " + e.getMessage()); 
      } 

      return comicBitmap; 
     } 

     @Override 
     protected void onPostExecute(Bitmap result) 
     { 
      File root = Environment.getExternalStorageDirectory(); 
      File file = new File(root.getAbsolutePath() + "/DCIM/tmpimg/cached/tmp/tmp.png"); 
      Picasso.with(context).load(file).into(canvas); 
      Log.i(ComicApplication.TAG, "FILE LOADED FROM : " + file.getAbsolutePath()); 
     } 
    } 
} 

내가 휴대 전화의 이미지 뷰어에서 tmp.png을 볼 수 있어요 :

onPostExecute는 난 그냥 비트 맵에서 생성 된 파일을 사용하여 이미지 뷰를로드. 나는 피카소의 끝에서 어떤 예외 나 오류도받지 않고있다.

피카소가 내 이미지를 파일에서로드하지 않는 이유에 대해 누군가 나를 도울 수 있습니까?

+1

Picasso.with (컨텍스트) .load ("file : //"+ file) .into (캔버스); –

+0

질문 편집에 로그를 추가 할 수 있습니까? –

답변

2

첫째, 올바른 파일 경로 Picasso을 제공하고 있는지 확인, 그런 다음 앱 Manifest.xml에 READ_EXTERNAL_STORAGE의 권한을 추가해야합니다 :

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 

는 매니페스트 수준

이 줄을 추가
+0

나는 이미 그 허가를 받았습니다. 보시다시피 디렉토리에 쓸 수 있습니다. 파일에 대한 읽기 및 쓰기 권한은 IMO의 운송용 말과 같습니다. –

+0

맞습니다. 잘못된 파일 경로를 제공하고 있습니다. "root.getAbsolutePath() +"/ DCIM/tmpimg/cached/tmp/tmp.png'가 아닌'root.getAbsolutePath() + "/ DCIM/tmpimg/cached/tmp.png'에 있어야합니다. –

+0

ok, can 이 답변을 귀하의 질문에 대한 해결책으로 표시해주십시오. –

관련 문제