2011-05-05 5 views
11

드로어 블 폴더에 많은 리소스가 있습니다 .5 백 킬로바이트 이상의 큰 크기가 있습니다. 한 번에 srollView에서 25 개의 이미지를 모두로드해야합니다. 평소처럼 나는 추억이 다 떨어졌습니다. 프로그래밍 방식으로 이미지의 크기를 줄일 수있는 방법이 있습니까?드로어 블에서 파일 만들기

이 함수가 있는데 매개 변수가 File이고 drawable에서 File을 만드는 방법을 모르겠습니다.

 

private Bitmap decodeFile(File f){ 
    Bitmap b = null; 
    try { 
     //Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 

     FileInputStream fis = new FileInputStream(f); 
     BitmapFactory.decodeStream(fis, null, o); 
     fis.close(); 

     int scale = 1; 
     if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) { 
      scale = Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE/(double) Math.max(o.outHeight, o.outWidth))/Math.log(0.5))); 
     } 

     //Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     fis = new FileInputStream(f); 
     b = BitmapFactory.decodeStream(fis, null, o2); 
     fis.close(); 
    } catch (FileNotFoundException e) { 
    } 
    return b; 
} 

나는 탐색 시스템이 메모리 부족을 보여주는하고 스택에서 다시 다른 뷰를 제거하고 다수의 후이 화면을 주기적으로 여러 번 thorugh 가야하지만 실제로 필요합니다. 도와주세요.

답변

31

당신은 다음 코드를 사용하여 당김 자원에서 InputStream를 열 수 있습니다

InputStream is = getResources().openRawResource(id); 
여기

id는 드로어 블 자원의 식별자입니다. 예 : R.drawable.abc

이제이 입력 스트림을 사용하여 파일을 만들 수 있습니다. 이 입력 스트림을 사용하여 파일을 만드는 방법에 대한 도움이 필요하면 알려주십시오.

업데이트 : 파일에 데이터를 기입하기 : 내가 @의 mudit의 대답에서 큐했다 및 입력 스트림에서 드로어 블을 생성

try 
    { 
    File f=new File("your file name"); 
    InputStream inputStream = getResources().openRawResource(id); 
    OutputStream out=new FileOutputStream(f); 
    byte buf[]=new byte[1024]; 
    int len; 
    while((len=inputStream.read(buf))>0) 
    out.write(buf,0,len); 
    out.close(); 
    inputStream.close(); 
    } 
    catch (IOException e){} 
    } 
+0

도움 주셔서 감사합니다. 이 오류가 발생합니다 ... InputStream is = (InputStream) getResources(). openRawResource (R.drawable.image); . 제발 파일을 만드는 방법도 말해. – James

+1

나는 이것을 이렇게 던져야했다. InputStream = (InputStream) getResources(). openRawResource (R.drawable.simplelist); – James

+0

"getResources"는 Activity 클래스에서 정의 된 함수이므로 입력 스트림을 가져 오는 코드를 작성한 곳의 활동 컨텍스트가 있는지 확인하십시오. – mudit

-1

. 나중에 드로어 블을 Adapter의 ImageView에로드합니다.

InputStream inputStream = mContext.getResources(). openRawResource (R.drawable.your_id);

Bitmap b = BitmapFactory.decodeStream(inputStream); 
b.setDensity(Bitmap.DENSITY_NONE); 
Drawable d = new BitmapDrawable(b); 
mImageView.setImageDrawable(d); 

Here는 솔루션

내가 this을 사용하여 선호 그래서 내가 바로 가기를 좋아
0

의 자세한 버전입니다.

drawable에서 파일을 creatting의 경우 this을 참조하십시오.

build.gradle

compile 'id.zelory:compressor:1.0.4' 

을이 넣어 그리고 당신은 압축하여 원하는 목적지 이미지가 README.md이 더 많은 정보를 제공

Bitmap compressedImageFile = Compressor.getDefault(context).compressToBitmap(your_file); 

을 넣어. 6 년 후 답변을 가져 오게해서 죄송합니다.

0

형제가 두 가지 방법

  1. 가장 쉬운 하나를 사용하는 일부 타사 라이브러리
  2. 하거나 드로어 블을 압축 Bitmap.createScaledBitmap 방법을 사용하여 그릴 수 아래로

확장 할 비트 맵 클래스를 사용

단계 :

// 1 단계 부하 당김과

Bitmap b = BitmapFactory.decodeResource(context , resId)

// 2 단계 재조정하여 비트 맵을 비트 맵으로 변환

Bitmap nBitmap = b.createScaledBitmap(getResources() , newHieght , newWidth , true); 

// 3 단계 비트 맵에서 드로어 블을 작성

BitmapDrawable drawable = new BitmapDrawable(nBitmap); 

이 방법은 매우 비싸기 때문에 3 부 라이브러리를 사용하는 것이 좋습니다

같은 https://github.com/Tourenathan-G5organisation/SiliCompressor